I have a student trying to filter the Cats dataset for a specific temperment, but the column for temperment includes several types of temperments for each cat (multiword string). If the user of the app chooses “Active” as temperment, how can we code the app to check inside each temperment string for the word “Active”? Use “substring” or “include”?
Greetings @bcarlson,
Depends on your use case, if the content you are looking for is in a fixed location index like char position 16 - 19 for each entry i would suggest using substring as you can compare direct matches
however if the position is not fixed includes may be the easiest solution. though keep in mind that when specifically searching for “cat” in “catastrophe” will match despite it specifically being the word your looking for, you could end up doing " cat " but that also has issues since you create a bunch of edge cases for yourself to go through
the best case solution is to use regex in your case but given it’s complexity i know you may wish to go with the previous option given but here’s the best cases solution
var activeCats = getColumn("cats", "active");
var filterActive = [];
for (var i = 0; i < activeCats.length; i++) {
var cat = activeCats[i];
// below are example solutions, pick one that fits your use case
// substring solution
if(cat.substring(16, 19) == "cat") {
filterActive.push(cat);
}
// include solution
if (cat.includes(" cat ")) {
filterActive.push(cat);
}
// best solution
if (cat.match(/\bcat\b/) !== null) {
filterActive.push(cat);
}
}
hope this helps
Varrience
1 Like