I have two arrays and want to filter the result from data array.
var data = [{"role":"Frontend", "languages": ["HTML", "CSS", "JavaScript"]},{"role":"Fullstack", "languages": ["JavaScript"]}]
var selectItem = ["CSS"];
Objects are added to selectItem[] once they data are selected from an UI.
I want to output by filtering from the data array against selectItem.
But the selectItem can have the values from both role and languages.
Like:
var selectItem = ["Frontend","CSS"];
I have tried to filter out the result:
var users = this.myJson.filter(
(el) =>
this.selectItem.includes(el.role) ||
el.languages.some((e1) => this.selectItem.indexOf(e1) >= 0)
);
console.log(users);
So, how can filter the data array with multiple keys? This query is filtering from the languages but not filtering the values from the role.
Update:
The query I tried was working but when the new item is added on the selectedItem like:
var selectItem = ["Frontend","CSS", "HTML"];
It is still returning all the values because it contains the CSS.
So I want to filter the result if it contains all the tags that is CSS and HTML and Frontend in the data.
var data = [
{"role":"Frontend", "languages": ["HTML", "CSS", "JavaScript"]},
{"role":"Fullstack", "languages": ["JavaScript"]},
{"role":"Frontend", "languages": ["CSS","JavaScript"]}
]
var selectItem = ["Frontend","CSS", "HTML"];
var users=data.filter(el => selectItem.length &&
(selectItem.includes(el.role) ||
el.languages.some(e1 => selectItem.includes(e1)) )
);
console.log(users);
My expected output is:
[{
languages: ["HTML", "CSS", "JavaScript"],
role: "Frontend"
}]
What is the best way of filtering with the multiple keys from array object?