'filter for nested arrays
I am trying to filter in a search for a value in a specific column and then return all the values.
But when I do that I get undefined instead. it works fine when it is not a nested array. This is the version that does not work.
data.filter(value =>{
let A = console.log(value?.cells[3]?.value?.map((x)=> {{x?.status}}))
//this returns undefined
console.log(A)
if(search =='')
{
return value;
}
else if
(value?.cells[3]?.value?.map((x)=> {x?.status})?.toLowerCase().includes(search?.toLowerCase()))
{
return value;
}
This is the schema of the json.
{
"Name": "value",
"results": [
{
"status": " status warning",
"check": " check sometextwiththecheck",
"results": " results sometextwiththeresultsofthecheck",
"_id": ObjectId("62725d9cc52b45ba21a39193")
},
{
"status": "2 status error",
"check": "2 check sometextwiththecheck",
"results": " 2 results sometextwiththeresultsofthecheck",
"_id": ObjectId("62725d9cc52b45ba21a39193")
}
]
}
and I am trying to filter by the array result with the key status.
This worked for me though
data.?.filter(value =>{
if(search =='')
{
return value;
}
else if
(value?.cells[0]?.value?.toLowerCase().includes(search?.toLowerCase()))
{
return value;
}
Solution 1:[1]
You can do:
const data = {
Name: 'value',
results: [
{
status: ' status warning',
check: ' check sometextwiththecheck',
results: ' results sometextwiththeresultsofthecheck',
_id: '62725d9cc52b45ba21a39193',
},
{
status: '2 status error',
check: '2 check sometextwiththecheck',
results: ' 2 results sometextwiththeresultsofthecheck',
_id: '62725d9cc52b45ba21a39193',
},
],
}
const search = 'err'
data.results = data.results.filter(({ status }) => status.includes(search))
console.log(data)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yosvel Quintero Arguelles |
