admin管理员组文章数量:1297099
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
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
Share edited Mar 25, 2017 at 7:21 Mihai Alexandru-Ionut 48.4k14 gold badges105 silver badges132 bronze badges asked Dec 13, 2016 at 20:05 PraveenPraveen 2451 gold badge5 silver badges18 bronze badges 1- 1 Have you taken a look at this answer? – gyre Commented Dec 13, 2016 at 20:07
1 Answer
Reset to default 4All you need is to eliminate each item from items
array whose employeeId
is in data1
,using splice
method.
References
splice method.
indexOf method
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);
本文标签: javascriptSplice or Remove list of elements from JSON ArrayStack Overflow
版权声明:本文标题:javascript - Splice or Remove list of elements from JSON Array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741647094a2390249.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论