remove item from array using its name / value

remove specific element from array javascript
javascript remove object from array by property
lodash remove item from array
react remove item from array by index
remove value from array php
remove object from json array javascript
javascript remove element from array using splice
typescript remove item from array

I have the following array

var countries = {};

countries.results = [
    {id:'AF',name:'Afghanistan'},
    {id:'AL',name:'Albania'},
    {id:'DZ',name:'Algeria'}
];

How can I remove an item from this array using its name or id ?

Thank you

Array.prototype.removeValue = function(name, value){
   var array = $.map(this, function(v,i){
      return v[name] === value ? null : v;
   });
   this.length = 0; //clear original array
   this.push.apply(this, array); //push all elements except the one we want to delete
}

countries.results.removeValue('name', 'Albania');

Find the index of the array element you want to remove using indexOf , and then remove that index with splice . The splice() method changes the contents of an  JavaScript provides many ways to remove elements from an array. You can remove an item: By its numeric index. By its value. From the beginning and end of the array. Removing an element by index. If you already know the array element index, just use the Array.splice() method to remove it from the array. This method modifies the original array by

Created a handy function for this..

function findAndRemove(array, property, value) {
  array.forEach(function(result, index) {
    if(result[property] === value) {
      //Remove from array
      array.splice(index, 1);
    }    
  });
}

//Checks countries.result for an object with a property of 'id' whose value is 'AF'
//Then removes it ;p
findAndRemove(countries.results, 'id', 'AF');

Removing Array Items By Value Using Splice. If you know the value you want to remove from an array you can use the splice method. First you  Removing Array Items By Value Using Splice. If you know the value you want to remove from an array you can use the splice method. First you must identify the index of the target item. You then use the index as the start element and remove just one element.

Try this:

var COUNTRY_ID = 'AL';

countries.results = 
  countries.results.filter(function(el){ return el.id != COUNTRY_ID; });

The best way to remove an element from an array based on the value in JavaScript is to find index number of that value in an array using indexOf() function and then delete particular index value using the splice() function. Here, We will show you how to works angular remove elements from an array. We will use angular remove elements…

Try this.(IE8+)

//Define function
function removeJsonAttrs(json,attrs){
    return JSON.parse(JSON.stringify(json,function(k,v){
        return attrs.indexOf(k)!==-1 ? undefined: v;
}));}
//use object
var countries = {};
countries.results = [
    {id:'AF',name:'Afghanistan'},
    {id:'AL',name:'Albania'},
    {id:'DZ',name:'Algeria'}
];
countries = removeJsonAttrs(countries,["name"]);
//use array
var arr = [
    {id:'AF',name:'Afghanistan'},
    {id:'AL',name:'Albania'},
    {id:'DZ',name:'Algeria'}
];
arr = removeJsonAttrs(arr,["name"]);

If you know the value. In this case, one good option is to use filter() , which offers a more declarative approach: const  The correct way to remove an item from an array is to use splice(). It takes an index and amount of items to delete starting from that index. It takes an index and amount of items to delete starting from that index.

You can delete by 1 or more properties:

//Delets an json object from array by given object properties. 
//Exp. someJasonCollection.deleteWhereMatches({ l: 1039, v: '3' }); -> 
//removes all items        with property l=1039 and property v='3'.
Array.prototype.deleteWhereMatches = function (matchObj) {
    var indexes = this.findIndexes(matchObj).sort(function (a, b) { return b > a; });
    var deleted = 0;
    for (var i = 0, count = indexes.length; i < count; i++) {
        this.splice(indexes[i], 1);
        deleted++;
    }
    return deleted;
}

Using this index value we will then want to actually remove the element, which we can do with the splice() method. function removeElement(array, elem) { var  Using ArrayList to remove element from an array If you want to remove element from an array using Collection API provided by the Java language then you can convert array to an ArrayList and then remove element from the ArrayList. Once the element is removed you can again convert the ArrayList to an array.

First, the index of the element in question is determined via indexOf() . Second, splice() is used to remove the array element at that index. We only  Or, remove all strings with a certain value: while(a.remove("acbd")) {} It's a bit more complicated, if you have more complex objects in your collection and want to remove instances, that have a certain property. So that you can't remove them by using remove with an object that is equal to the one you want to delete.

The delete operator only erases the value of the element and makes the element as undefined. The length of the array stays unaffected. Now if we pass this bool array to [] operator of numpy array arr, then it will select the elements from arr foe which bool array has True at corresponding index. Basically it returns the elements from arr which are not 6. Another point to be noted is that it returns a copy of existing array with elements with value 6.

The correct way to remove an item from an array is to use splice() . It takes an index and amount of items to delete starting from that index. 1 2 3 4 5 6 > var array  To delete an item from the page in React, you need to take a few steps. In our example we first had to deal with raising and handling events between child and parent components. Then we saw that when handling the event in the parent, we used the filter function to create a new array of items that did not include the one that was clicked.

Comments
  • +1: This isn't the highest voted answer, but it worked best for me. I was parsing a JSON array that I was getting back from a jquery AJAX success handler, and the $.each method was unexpectedly tripping over 'undefined' values. I'm still not sure why I was getting back 'undefined' values to begin with; but in any event, this code snippet definitely worked best for me. Thanks!
  • @JimG.: Glad I could be of help :-)
  • @JimG The undefined values you get are because the indexes changed after the array elements have been spliced out, so the accepted answer doesn't actually work. Can you change it to this one]
  • @GX.: Can you please change the accepted answer to this answer?
  • Just a heads up, if the value of the property of the JSON object you're deleting is a number(like: {"key": 1}), make sure you cast the parameter you're passing into the function to a number: removeValue('key', +value); this drove me nuts for a couple of hours.
  • Note: jQuery is necessary to use this function
  • Does this not break the index, because the index changes during execution if an element is removed.
  • @JohnStrickler: can you rewrite this without jquery please. I don't like librairies.
  • According to the documentation api.jquery.com/each, $.each doesn't take two arguments (anymore?).
  • Thanks. Works like a charm :-)
  • +1: Worth noting that it isn't supported in IE < 9
  • @Jeremy Heiler: for IE such function can be added from code provided here: developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…
  • You referenced the same link I referenced :-P
  • I think he wants to delete the entire object from the array. Instead of deleting the property from the object.
  • Delete should only be used for removing properties from objects, NOT for deleting things in an array - it merely replaces the value at that index with undefined! (Does not change length of array, etc). See stackoverflow.com/questions/5767325/…