简体   繁体   中英

How to remove a nested JSON element?

My JSON looks like this.

var data =  [{
        "ID"                  : 3,
        "discRec"             : "Some sample record",
        "Tasks"               : [{
                                "ID"          : 7,
                                "discParcel"  : ["string1", "string2"]
                                }]
        }, 
        {
        "ID"                  : 4,
        "discRec"             : "Some sample record 2",
        "Tasks"               : [{
                                "ID"          : 8,
                                "discParcel"  : ["string3", "string4"]
                                }]
        }];

Now, i want to remove a first element of 'discParcel' which has ID attr as '8' which has parent ID attr as '4. How to do that in angular 2. Can anybody help.

data
    .find(record => record.ID === 4)
    .Tasks.find(task => task.ID === 8)
    .discParcel.shift();

Assuming data is a valid JSON you do it like you would in plain javascript :

let arr = JSON.parse(data);
arr.forEach(e => {
  if(e.ID  === 4){
    Tasks.forEach(el => {
      if(el === 8){
        discParcel.shift();
      }
    })  
  }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM