I have the object with values. I trying to filter based on values.
var data = {
"map": {
"0": ["abcd"],
"1": ["efgh"],
"2": ["xyz"],
}
}
In above object I am trying to filter xyz which are have in array values.
Expecting output:
{ '0': [ 'xyz' ] }
I done the code but I am not sure it's efficient way. Can someone help me this.
const data = {
"map": {
"0": ["abcd"],
"1": ["efgh"],
"2": ["xyz"],
"3": ["abcd", "xyz"],
}
}
const filtered = Object.keys(data.map)
.filter((key) => {
return data.map[key].includes('xyz');
})
.reduce((obj, key, cIndex) => {
obj[cIndex] = data.map[key];
return obj;
}, {});
console.log('filtered', filtered);
0ton, and the order should also be kept in output? \$\endgroup\$