简体   繁体   English

拼接或删除JSON数组中的元素列表

[英]Splice or Remove list of elements from JSON Array

I am having a JSON Array Output from REST API like this , I am displaying this items on the HTML using ng-repeat. 我从REST API这样获得了JSON Array Output,我正在使用ng-repeat在HTML上显示此项目。

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 . 当用户尝试使用selectall / single select删除时,我正在调用REST API来从db中删除员工ID。 once i get a successful response i am planning to splice / remove the values that have been selected by the user from the VIEW. 一旦获得成功的响应,我计划拼接/删除用户从VIEW中选择的值。 I would like to remove the following employeeid and their type,alive removed from the searchresponse 我想删除以下employeeid及其类型,将其从搜索响应中删除

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

Whatever the data1 has corresponding details should be removed from the searchresponse 数据1中具有相应详细信息的任何内容均应从搜索响应中删除

All you need is to eliminate each item from items array whose employeeId is in data1 ,using splice method. 您需要做的就是使用splice方法从employeeIddata1 items数组中消除每个项目。

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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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