1

I am having a JSON Array Output from REST API like this , I am displaying this items on the HTML using ng-repeat.

var searchresponse = [{
    "items": [{
        "employeeId": "ABC",
        "type": "D",
        "alive": "Yes"

    }, {
        "employeeId": "DEF",
        "type": "D",
        "alive": "Yes"

    }, {
        "employeeId": "NPK",
        "type": "D",
        "alive": "Yes"

    }, {
        "employeeId": "PKN",
        "type": "A",
        "alive": "Yes"
    }],
    "more": false
}];

when user tries to delete using selectall/single select i am calling a REST API to remove the employee id from the db . once i get a successful response i am planning to splice / remove the values that have been selected by the user from the VIEW. I would like to remove the following employeeid and their type,alive removed from the searchresponse

 var data1=["ABC","NPK"];

Whatever the data1 has corresponding details should be removed from the searchresponse

Mihai Alexandru-Ionut
  • 41,021
  • 10
  • 77
  • 103
Praveen
  • 225
  • 1
  • 3
  • 16
  • 1
    Have you taken a look at [this answer](http://stackoverflow.com/questions/5767325/how-to-remove-a-particular-element-from-an-array-in-javascript)? – gyre Dec 13 '16 at 20:07

1 Answers1

2

All you need is to eliminate each item from items array whose employeeId is in data1,using splice method.

References

var searchresponse = [{
    "items": [{
        "employeeId": "ABC",
        "type": "D",
        "alive": "Yes"

    }, {
        "employeeId": "DEF",
        "type": "D",
        "alive": "Yes"

    }, {
        "employeeId": "NPK",
        "type": "D",
        "alive": "Yes"

    }, {
        "employeeId": "PKN",
        "type": "A",
        "alive": "Yes"
    }],
    "more": false
}];
var data1=["ABC","DEF"];
var items=searchresponse[0].items;
var i=items.length;
while (i--) {
    if(data1.indexOf(items[i].employeeId)!=-1){
        items.splice(i,1);
    }
}
console.log(searchresponse[0].items);
Mihai Alexandru-Ionut
  • 41,021
  • 10
  • 77
  • 103