从JSON Array中拼接或删除元素列表

时间:2016-12-13 20:05:53

标签: javascript jquery angularjs arrays json

我正在使用REST API的JSON数组输出,我正在使用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
}];

当用户尝试使用selectall / single select删除时,我正在调用REST API以从db中删除employee id。一旦我得到成功的回复,我计划拼接/删除用户从VIEW中选择的值。我想删除以下employeeid及其类型,从searchresponse

中删除
 var data1=["ABC","NPK"];

无论data1具有相应的详细信息,都应从searchresponse中删除

1 个答案:

答案 0 :(得分:2)

您只需使用items方法消除employeeId data1位于splice的{​​{1}}数组中的每个项目。

参考文献

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