How to pass string literal to filter function in JavaScript
I want to pass string literal to filter function.
The result should be
filter = data.filter(o => o.tag.toLowerCase().indexOf(("website").toLowerCase()) != -1 &&
o.tag.toLowerCase().indexOf(("phone").toLowerCase()) != -1
What I am currently doing is
- Get all labels in an array using for loop
- Form string query and execute filter in array
- The problem is when the filter is passed, it returns everything, which means string literals don't work
for (let i = 0; i < values.length; i++) {
if (i + 1 >= values.length) {
query = query.concat(` o.tag.toLowerCase().indexOf(("${values[i]}").toLowerCase()) != -1 `);
} else {
query = query.concat(` o.tag.toLowerCase().indexOf(("${values[i]}").toLowerCase()) != -1 && `);
}
}
let filter = data.filter(o => `${query}`);
debugger;
The sample snippet I am trying to filter. I want to make this filter function dynamic
data=[{name:"fpl.xlsx",author:"hello",hits:6,date:"2020-01-01",tag:"logo,website"}
,{name:"corporate.pptx",author:"hellob",hits:1,date:"2020-02-01",tag:"logo"},
{name:"index.html",author:"hellob",hits:7,date:"2020-02-02",tag:"logo,abc"}
]
let filter=[];
filter=data.filter(o=>o.tag.indexOf("logo")!=-1 && o.tag.indexOf("abc")!=-1)
console.log(filter);
If you want to make sure that all the values
matches that are in one project tag
, then you can use to do that.Array#every
const arr1 = ["aardvark", "ant", "alien"];
const arr2 = ["apple", "audi", "bcoccoli"];
const startsWithA = word => word[0] === "a";
console.log(arr1.every(startsWithA));
console.log(arr2.every(startsWithA));
Also, this method can be used instead, since it returns a boolean value directly.String#includes
.indexOf
const word = "applesauce";
console.log(word.includes("apple"));
console.log(word.includes("sauce"));
So we get this:
const data = [{name:"fpl.xlsx",author:"hello",hits:6,date:"2020-01-01",tag:"logo,website"}
,{name:"corporate.pptx",author:"hellob",hits:1,date:"2020-02-01",tag:"logo"},
{name:"index.html",author:"hellob",hits:7,date:"2020-02-02",tag:"logo,abc"}]
const values = ["logo", "abc"];
let result = data
.filter(
({tag}) => values.every(
value => tag.toLowerCase()
.includes(value.toLowerCase())
)
)
console.log(result);
However, tag
you may get false positives due to the inclusion of a comma-separated list:
const tag1 = "foobar";
const tag2 = "foo,bar";
const values = ["foo", "bar"];
console.log(values.every(value => tag1.includes(value)));
console.log(values.every(value => tag2.includes(value)));
A token might be a word that is made up of multiple other words that match, and you just want to do an exact match. You can separate the labels with , and you just need to find if the two arrays overlap:String#split
const tag1 = "foobar";
const tag2 = "foo,bar";
const values = ["foo", "bar"];
const arrayOfTag1 = tag1.split(",");
const arrayOfTag2 = tag2.split(",");
console.log(values.every(value => arrayOfTag1.includes(value)));
console.log(values.every(value => arrayOfTag2.includes(value)));
Collections can be used to reduce the complexity of the overall lookup, in this case filtering looks like this:
const data = [{name:"fpl.xlsx",author:"hello",hits:6,date:"2020-01-01",tag:"logo,website"}
,{name:"corporate.pptx",author:"hellob",hits:1,date:"2020-02-01",tag:"logo"},
{name:"index.html",author:"hellob",hits:7,date:"2020-02-02",tag:"logo,abc"}]
const values = ["logo", "abc"];
let result = data
.filter(
({tag}) => {
const lookupTags = new Set(
tag
.split(",")
.map(x => x.toLowerCase())
);
return values.every(value => lookupTags.has(value.toLowerCase())
)
})
console.log(result);