0

I have following nested array, I need to delete fields object with id 47 and return the whole parent array. How this can be done in JS?

[{
    "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 have tried following, but making mistake with the use of filter function.

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;    
  }
2
  • your expected output? Commented May 27, 2021 at 12:01
  • Its done with the help of following answers. Commented May 27, 2021 at 12:13

2 Answers 2

1

You can use splice instead of filter to remove the object from array.

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.splice(0,1);
      } else if(ele[searchKey]){
         this.getUpdatedData(ele[searchKey], id, searchKey);
      }      
   }.bind(this));
   return data;    
};
Sign up to request clarification or add additional context in comments.

Comments

0

You can made it by using recursive filter function.

data = [{
    "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"
        }
    ]
}]

const filterId = (data, id) => {
  if(!Array.isArray(data))
    return data
    
  return data.filter(item => {
    if('fields' in item)
        item.fields = filterId(item.fields, id)
    return (item.id === undefined || item.id !== id)
  })
}
const filtredData = filterId(data, 47)
console.log(JSON.stringify(filtredData, null, 4))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.