Check object array index by property

Asked

Viewed 636 times

0

When you click on an element, I get an id. With this I do it in an array and find out which object has the same id, and need to remove all objects with the same id.

 function onRemove(city) {
    for (var i = 0; i < array.length; i++) {
        if (array[i].id === city.id){
            (remover objeto do array)
         }
     }
  }
  • 1

    Don’t put the solution in the question. That’s not how Stackoverflow works. If @Ricardopunctual provided the solution you wanted then mark it as right already indicates it is solution. Otherwise and if you think it is something different, you can even answer your question with what you consider solution.

  • Perfect! Thank you!

4 answers

2


You can use splice to remove an element per index from an array:

array.splice(i, 1);

In the example, it removes 1 element from the index i

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

EDIT: An example given the comments:

  function onRemove(city) {
      var removeu = true;
      while(removeu) {
         removeu = remover(city);
      }
  }

  function remover(city) {
     for (var i = 0; i < array.length; i++) {
        if (array[i].id === city.id){
            array.splice(i, 1);
            return true;
         }
     }
     return false;
  }

EDIT 2: a solution using Array.filter:

var novoarray = array.filter(function(a){return a.id != city.id});

Example:

var array = [ {id: 1, city: 'AAA'}, {id: 2, city: 'BBB'}, {id: 3, city: 'CCC'} ];
var city = {id: 2, city: 'BBB'};

var novoarray = array.filter(function(a){return a.id != city.id});
console.log(novoarray);

  • Thanks for the reply, I tried it, but each comes q I remove a value from the array the value of i decreases and does not end up sweeping in the array until the end

  • There are two points here: 1) do you want to search for a specific item, which will only have one and remove? If it is just use one break and close the for 2) There may be several items and you want to remove them all. In this other case, you need to make several A calls Function and change it to return something that says it has no more items to remove. From there, you will call until you remove all items, using the same solution with break to leave each time you find an item and remove

  • Alternative 2, will have several, and then need to remove all objects that have the same id within the array.

  • example : var remover=true; while(remover) { remover= functionRemover(city); }

  • I edited the answer with an example of this logic

  • Good Ricardo! That filter was what I needed!

Show 1 more comment

1

You can use the splice, and when you find an occurrence, you decrease -1 in the i:

var array = [
   {
   "id":"1"
   },
   {
   "id":"2"
   },
   {
   "id":"1"
   },
   {
   "id":"4"
   },
   {
   "id":"1"
   },
   {
   "id":"1"
   },
   {
   "id":"1"
   }
   ];

var city = { "id":"1" };

for (var i = 0; i < array.length; i++) {
  if (array[i].id === city.id){
      array.splice(i, 1);
      i--;
   }
}
  
console.log(array);

0

Try this way (UPDATE: I upgraded to ES5 and also to delete several):

function onRemove(city) {
    var citiesFound = array.filter(function (a) {
        return a.id === city.id;
    });

    citiesFound.forEach(function(c) {
        var index = array.indexOf(c);

        if (index >= 0) {
            array.splice(index, 1);
        }
    });
}
  • Thanks for the answer! But I need to use only ecmascript-5, I’ll still do a test

  • I edited for ES5, I hope you get it, thanks.

  • I played on Babel and gave this result tbm, I did the test, but when there is more than one object with the same id it does not remove all... Thanks for the help

  • Ready @Brunocabral, edited to suit more than one item with the same id

  • Thanks Leandro! I used the filter that Ricardo Punctual gave me in the answers above and solved!

  • var cityFound = array.filter(function (a) {&#xA; return a.id != city.id;&#xA; });&#xA; array = cityFound;

Show 1 more comment

0

Hello,

You can use the indexOf to identify the position where its value (city id) is.

After that, you can remove the item with splice. In it you indicate the position where you want to start removing, and the number of items you want to remove from that (1 in your case, I believe). In addition, the return of splice is the new array with the removals done. It would look something like this:

function onRemove(city) {
    var posicao = array.indexOf(city);
    array.splice(posicao, 1);
}

Note: browser support for indexOf is limited. Not supported in Internet Explorer 7 and 8.

Of course if the indexOf does not work very well for you, it is possible to locate the city in question the way you did (sweeping with the for). However, I believe that the splice be helpful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.