I am seeking assistance in filtering nested Objects by property values, with a specific requirement involving values stored in an array. Despite previous similar inquiries, I have yet to find a solution tailored to cases like mine.
In reviewing the code snippet provided, my objective is to filter objects based on specific tags. Specifically, I aim to retrieve objects that contain both "a" and "b" within their tags array.
const input1 = {
"0":{
"id":"01",
"name":"item_01",
"tags":["a","b"],
},
"1":{
"id":"02",
"name":"item_02",
"tags":["a","c","d"],
},
"2":{
"id":"03",
"name":"item_03",
"tags":["a","b","f"],
}
}
function search(input, key) {
return Object.values(input).filter(({ tags }) => tags === key);
}
console.log(search(input1, "a"));
The desired outcome consists of the following:
{
"0":{
"id":"01",
"name":"item_01",
"tags":["a","b"],
},
"2":{
"id":"03",
"name":"item_03",
"tags":["a","b","f"],
}
}
Your help is greatly appreciated!