最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove all objects from array with a specific key value - Stack Overflow

programmeradmin0浏览0评论

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
 |  Show 3 more ments

2 Answers 2

Reset to default 8

filter() 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);
发布评论

评论列表(0)

  1. 暂无评论