Can anyone help me figure out how to delete the "fields" object with id 47 from this nested array using JavaScript and return the entire parent array?
[{
"id": 10,
"name": "phone",
"fields": [
{
"id": 31,
"name": "cellphone",
"fields": [
{
"id": 47,
"name": "android"
}
]
},
{
"id": 32,
"name": "landline"
}
]
},
{
"id": 12,
"name": "document"
},
{
"id": 90,
"name": "document-name",
"fields": [
{
"id": 91,
"name": "new-name"
}
]
}]
I've attempted to solve this issue using a filter function, but it seems I'm making a mistake in the implementation. Can somebody provide guidance?
private getUpdatedData(data, id, searchKey = 'fields') {
data.forEach(function(ele) {
if(ele.id === id && ele[searchKey]) {
ele = ele[searchKey].filter(s => s.id === id);
} else if(ele.id === id && !ele[searchKey]) {
data = data.filter(s => s.id === id)
} else if(ele[searchKey]){
this.getUpdatedData(ele[searchKey], id, searchKey);
}
}.bind(this));
return data;
}