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

时间:2021-12-14 21:26:14

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数组输出,我正在使用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 . 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

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

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

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

无论data1具有相应的细节,都应该从searchresponse中删除

1 个解决方案

#1


1  

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

您只需要使用splice方法从employeeId在data1中的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);

#1


1  

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

您只需要使用splice方法从employeeId在data1中的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);