I need to implement a filter for an array of nested objects in JavaScript.
After researching, I found the following solution from another source.
var sampleData= [{
"rowId": "3.0.0",
"startDate": "2020-10-20",
"subTasks": [
{
"rowId": "3.3.0",
"startDate": "2021-05-26",
"subTasks": [
{
"rowId": "3.3.0.1",
"startDate": "2021-05-26",
"subTasks": []
},
{
"rowId": "3.3.0.2",
"startDate": "2021-06-09",
"endDate"": "2021-07-23",
"subTasks"]<span></span>: []
},
]
},
]
}]
filtered = sampleData.map(element => {
return {
...element,
subTasks: element.subTasks.filter(subElement => {
return subElement.endDate
})
}
})
console.log("sampleData",JSON.stringify(filtered))
The goal is to filter based on the end date. Expected result: Object with rowId "3.3.0.2" should be filtered out.
This current implementation only filters up to 2 levels deep. However, considering that my nested objects can extend up to 10 levels, how can I apply this filtering across any number of levels?