Seeking assistance with filtering results from the data
array using two arrays.
var data = [{"role":"Frontend", "languages": ["HTML", "CSS", "JavaScript"]},{"role":"Fullstack", "languages": ["JavaScript"]}]
var selectItem = ["CSS"];
Objects are added to selectItem[] after selection from a UI in data
.
The goal is to output by filtering from the data
array against selectItem
.
The challenge lies in filtering based on values from both role
and languages
in selectItem
.
For example:
var selectItem = ["Frontend","CSS"];
An attempt has been made 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);
How can the data
array be filtered with multiple keys, considering both languages
and role
?
Update:
The current query works but struggles when new items are added to selectItem
, like:
var selectItem = ["Frontend","CSS", "HTML"];
It returns all values as it contains CSS
. Looking to filter only if CSS
, HTML
, and Frontend
are present in 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);
Expected output:
[{
languages: ["HTML", "CSS", "JavaScript"],
role: "Frontend"
}]
Looking for the best approach to filter with multiple keys from an array object.