1

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

8
  • Post your data. 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. Commented Apr 16, 2018 at 17:04
  • If it is truly an object then than it shouldn't be possible to have duplicate keys. Commented Apr 16, 2018 at 17:04
  • I'll post the stringified data in a moment Commented Apr 16, 2018 at 17:05
  • 1
    Possible duplicate of Remove Object from Array using JavaScript Commented Apr 16, 2018 at 17:12

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

This obviously doesn't work as it deleted only one instance in an array
This is strange.
I know. I've figured it out now. Thanks
An Important thing I learned was that the filter is already a loop. It's going to loop through the whole array and delete anything that matches this case. I had my filter in a for loop through the array previously. That didn't work, this solution does work for me.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.