10

If I have something like

[Object(id:03235252, name:"streetAddress"), Object(id:32624666, name:"zipCode")...] 

How can I remove an object from that array that has name set to "zipCode"?

1
  • 4
    should be correctly written: [{id: 03235252, name: "streetAddress"}, {id: 32624666, name: "zipCode"}]
    – ianaz
    CommentedOct 31, 2012 at 14:51

5 Answers 5

15

If you need to modify the existing Array, you should use splice().

for (var i = array.length - 1; i > -1; i--) { if (array[i].name === "zipCode") array.splice(i, 1); } 

Notice that I'm looping in reverse. This is in order to deal with the fact that when you do a .splice(i, 1), the array will be reindexed.

If we did a forward loop, we would also need to adjust i whenever we do a .splice() in order to avoid skipping an index.

    10
    arr = arr.filter(function (item) { return (item.name !== 'zipCode'); }); 
    2
    • 3
      Probably worth mentioning that it doesn't work in IE 8 so you might have to shim it.
      – Daff
      CommentedOct 31, 2012 at 14:47
    • 1
      Should be noted that this doesn't remove the object from the Array. It builds a new Array and replaces the old one, which could be an issue if there are multiple references to the same Array.CommentedOct 31, 2012 at 14:52
    3

    Updated suggestion

    Updated this answer due to doing prototypes on arrays are bad prac so to get people who use the suggestion to write better code here is a better option:

    const myArr = [ { name: "lars", age: 25 }, { name: "hugo", age: 28 }, { name: "bent", age: 24 }, { name: "jimmy", age: 22 } ]; const findAndRemove = (array, prop, value) => { return array.filter((item) => item[prop] !== value); } const newArr = findAndRemove(myArr, 'name', 'jimmy') console.log(newArr) // Could also be simply written like this: const otherArr = myArr.filter(item => item.name !== 'jimmy') 

    New code can be found and tested here

    Old suggestion

    This can also be done with a prototype on the array

    Array.prototype.containsByProp = function(propName, value){ for (var i = this.length - 1; i > -1; i--) { var propObj = this[i]; if(propObj[propName] === value) { return true; } } return false; } var myArr = [ { name: "lars", age: 25 }, { name: "hugo", age: 28 }, { name: "bent", age: 24 }, { name: "jimmy", age: 22 } ]; console.log(myArr.containsByProp("name", "brent")); // Returns false console.log(myArr.containsByProp("name", "bent")); // Returns true 

    Code can also be found and tested here

      2
      var i = array.length; while(i-- > 0) { if (array[i].name === "zipCode") array.splice(i, 1); } 
      • Loop through the array backwards (so you won't have to skip indexes when splicing)
      • Check each item's name if it's "zipCode"
        • If it is, splice it off using yourArray.splice(index,1);

      Then either:

      • continue if there is a possibility of having more than one name having the value "zipCode"
      • break the loop
        0

        This may be a detailed and easy solution.

        //plain array var arr = ['a', 'b', 'c']; var check = arr.includes('a'); console.log(check); //returns true if (check) { // value exists in array //write some codes } // array with objects var arr = [ {x:'a', y:'b'}, {x:'p', y:'q'} ]; // if you want to check if x:'p' exists in arr var check = arr.filter(function (elm){ if (elm.x == 'p') { return elm; // returns length = 1 (object exists in array) } }); // or y:'q' exists in arr var check = arr.filter(function (elm){ if (elm.y == 'q') { return elm; // returns length = 1 (object exists in array) } }); // if you want to check, if the entire object {x:'p', y:'q'} exists in arr var check = arr.filter(function (elm){ if (elm.x == 'p' && elm.y == 'q') { return elm; // returns length = 1 (object exists in array) } }); // in all cases console.log(check.length); // returns 1 if (check.length > 0) { // returns true // object exists in array //write some codes } 

          Start asking to get answers

          Find the answer to your question by asking.

          Ask question

          Explore related questions

          See similar questions with these tags.