Retrieve the index of an object with a certain attribute inside an array of objects

0

I have the following in javascript:

var listaImagens = [{"idImagem":4,"arquivo":"illustration_transport-04.svg","idCategoria":1,"nomeCategoria":"Transportes","modificacoes":[{"id":"sxqae3wtj1h2rzfr","cor":"#ff6500"},{"id":"egltjnqi7ut2zkt9","cor":"#FFFFFF"},]},{"idImagem":5,"arquivo":"casa.svg","idCategoria":2,"nomeCategoria":"Construcoes","modificacoes":[{"id":"aksjdhauhkuhksee","cor":"#fcdd10"},{"id":"lasdweiefnswekej","cor":"#FFCD00"},]};

How do I find the index of the object that has "category_id" = 2.

Any suggestions?

    
asked by anonymous 11.09.2014 / 15:49

3 answers

1
indexes = $.map(listaImagens, function(obj, index) {
    if(obj.idCategoria == 2) {
        return index;
    }
})

firstIndex = indexes[0]

SO gringo source: link

    
11.09.2014 / 15:59
1

An option I would take:

var indice = listaImagens.indexOf(listaImagens.filter(function(obj){
    return obj.idCategoria == 2;
})[0]);

JSFiddle: link

1. Explanation

Function filter () [ Array.prototype.filter ]

It is available throughout Array. It receives as first parameter a function; this function is called callback , and is executed (by the function filter() ) once for each element of the array. The filter() function outputs the array itself, but filtered .

The callback function is who performs this filtering, returning true (hold) or false (cut) to each element of the array. This function is based on up to 3 parameters (below) for this choice, but in my case I chose to receive only the first function from the filter() function:

  • [Required] current array element (in my case, obj );
  • [Optional] index of this element in the array;
  • [Optional] a reference to the array being traversed;

In my example, I made only the elements with the condition obj.idCategoria == 2 returned true and therefore deleted the remaining elements of the array.

IndexOf () function [ Array.prototype.indexOf ]

This function returns the index of an element inside the array. To do this, it strictly determines (% with_%) the equality of the parameter entered with each element of the array. In the case of literal objects, as is the case here, the strict comparison will only return% with% if the parameter is an reference to an element of the array. For this reason, it was necessary to perform the filtering ( === ) and the selection of the first element of the post-filter array ( true ).

2. Resolution of this problem in the near future

The future holds an even more practical method of discovering the index of an element within an array. Firefox was the first to run ahead and implement a function proposed in ECMAScript6, called filter() , and also available in the Array prototype [ Array.prototype.findIndex ].

It works very similarly to the functions [0] and findIndex() , receiving a callback function as a parameter that will be executed in sequence by each element of the array; as soon as a callback execution returns map() , the element index is returned by the filter() function.

In this way, the code could be further reduced to:

var indice = listaImagens.findIndex(function(obj){
    return obj.idCategoria == 2;
});

Once again, the implementation of this function has not been completed in most browsers , including the latest versions of Google Chrome.

    
11.09.2014 / 18:10
0

You can use the filter or forEach of the array. The advantage of the first one is that you can get the full item:

var auxIndex = -1;

// com filter
var item = listaImagens.filter(function(i, idx) {
    if(i.idCategoria == 2) {
        auxIndex = idx;
        return true;
    }
    return false;
})[0];

// com forEach
listaImagens.filter(function(i, idx) {
    if(i.idCategoria == 2)
        auxIndex = idx;
});

In both functions, i is the current element and idx is its index.

    
11.09.2014 / 16:01