admin管理员组文章数量:1318983
I've been literally struggling for a day, literally a whole day searching the entire stackoverflow and google to try and solve this issue but I'm desperate. I've tried dozens of solutions but none of them seem to work...
I've got an array of objects where each object has a key named pid with a certain value. Now I'd like to delete all objects with the same, specific pid value.
I've tried
forEach, filter, $.each, $.grep
and many other functions to try to solve this issue, all of them unsuccessful (or maybe I'm doing something wrong every time?)
Simply, I want to remove each object with a specific pid value. My current code is:
$.each(cart, function(i){
if(cart[i].pid === pid){
cart.splice(i,1);
}
});
But this one keeps throwing: Cannot read property pid of undefined
Other functions delete only a (random?) amount of objects, there are still some left overs in the array with the unwanted pid value.
I don't necessarily have to stick with the $.each
function, so any solution is greatly appreciated.
Array of objects:
[{"id":1523898500862,"amm":1,"type":"t","name":"bluecheese","pid":1523898494726,"cost":0.5},{"id":1523898501937,"amm":1,"type":"t","name":"edam","pid":1523898494726,"cost":0.5},{"id":1523898505766,"amm":1,"type":"t","name":"mozzarella","pid":1523898494726,"cost":1}]
As you can see all the three objects hold the same pid value, I want to delete them all, according to that pid
I've been literally struggling for a day, literally a whole day searching the entire stackoverflow and google to try and solve this issue but I'm desperate. I've tried dozens of solutions but none of them seem to work...
I've got an array of objects where each object has a key named pid with a certain value. Now I'd like to delete all objects with the same, specific pid value.
I've tried
forEach, filter, $.each, $.grep
and many other functions to try to solve this issue, all of them unsuccessful (or maybe I'm doing something wrong every time?)
Simply, I want to remove each object with a specific pid value. My current code is:
$.each(cart, function(i){
if(cart[i].pid === pid){
cart.splice(i,1);
}
});
But this one keeps throwing: Cannot read property pid of undefined
Other functions delete only a (random?) amount of objects, there are still some left overs in the array with the unwanted pid value.
I don't necessarily have to stick with the $.each
function, so any solution is greatly appreciated.
Array of objects:
[{"id":1523898500862,"amm":1,"type":"t","name":"bluecheese","pid":1523898494726,"cost":0.5},{"id":1523898501937,"amm":1,"type":"t","name":"edam","pid":1523898494726,"cost":0.5},{"id":1523898505766,"amm":1,"type":"t","name":"mozzarella","pid":1523898494726,"cost":1}]
As you can see all the three objects hold the same pid value, I want to delete them all, according to that pid
Share Improve this question edited Apr 16, 2018 at 17:13 Steven Dropper asked Apr 16, 2018 at 17:02 Steven DropperSteven Dropper 4673 silver badges15 bronze badges 8- Post your data. – Nenad Vracar Commented Apr 16, 2018 at 17:04
-
Please, provide a Minimal, Complete, and Verifiable example, example data of the
cart
variable will make it easier to test. – raul.vila Commented Apr 16, 2018 at 17:04 - If it is truly an object then than it shouldn't be possible to have duplicate keys. – Ryan Schaefer Commented Apr 16, 2018 at 17:04
- I'll post the stringified data in a moment – Steven Dropper Commented Apr 16, 2018 at 17:05
- 1 Possible duplicate of Remove Object from Array using JavaScript – Narendra Jadhav Commented Apr 16, 2018 at 17:12
2 Answers
Reset to default 8filter()
is your solution. It takes a callback as an argument that decides if the element should be kept in the array and returns the filtered array.
function remove(arr, pid) {
return arr.filter(e => e.pid !== pid);
}
let arr = [{ pid: 1 }, { pid: 2 }];
console.log("Removed pid:1", remove(arr, 1));
console.log("Removed pid:2", remove(arr, 2));
console.log("Removed pid:3", remove(arr, 3));
let yourArr = [{
"id": 1523898500862,
"amm": 1,
"type": "t",
"name": "bluecheese",
"pid": 1523898494726,
"cost": 0.5
}, {
"id": 1523898501937,
"amm": 1,
"type": "t",
"name": "edam",
"pid": 1523898494726,
"cost": 0.5
}, {
"id": 1523898505766,
"amm": 1,
"type": "t",
"name": "mozzarella",
"pid": 1523898494726,
"cost": 1
}];
console.log("Removed from your array", remove(yourArr, 1523898494726));
Here's a simple way which avoids any difficulties of modifying-while-iterating and good runs in good old O(n):
var removeByPid = function(cart, pid) {
var result = [];
for (var i = 0, len = cart.length; i < len; i++)
if (cart[i].pid !== pid) result.push(cart);
return result;
};
Now you can say:
var cart = /* ... get values for cart somehow ... */
// ... Do more stuff ...
cart = removeByPid(cart, 1234);
本文标签: javascriptRemove all objects from array with a specific key valueStack Overflow
版权声明:本文标题:javascript - Remove all objects from array with a specific key value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742053279a2418162.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论