I need to filter nested Objects by property values. I know similar questions have been asked before, but I couldn't find a solution for a case where the values were stored in an array.
In the provided code sample, I will need to filter the object based on tags. I would like to get objects which include "a" and "b" in the 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"));
As an output, I would like to receive the fallowing:
{
"0":{
"id":"01",
"name":"item_01",
"tags":["a","b"],
},
"2":{
"id":"03",
"name":"item_03",
"tags":["a","b","f"],
}
}
Thanks a lot in advance!