I have a scenario where I need to filter out specific arrays of objects from an array based on their id values. However, the current solution is returning an empty array when there is no match for the filter condition.
Any suggestions on how to resolve this issue?
const resKey = 'ABC';
const data = [
[{
"user": {
"firstName": "John",
"lastName": "Smith",
},
"id": "ABC"
}],
[{
"user": {
"firstName": "John",
"lastName": "Smith",
},
"id": "DEF"
}]
];
const result = data.map(eachArr => eachArr.filter(eachObj => eachObj.id === resKey));
console.log(result);
Desired Output:
[
[
{
user: {
firstName: "John",
lastName: "Smith",
},
id: "ABC",
},
],
]
Current Output:
[
[
{
user: {
firstName: "John",
lastName: "Smith",
},
id: "ABC",
},
],
[],
]