Object and Like Array search in Javascript to return the Index find

Asked

Viewed 271 times

0

I recently asked this question Object and Like Array Search in Javascript and it was answered to me in the best possible way, I am studying the documentation but with some difficulties where I came across this in the case, I can get the value I want using the filter and the map according to the answer of the question, but there arose another difficulty, and for me to bring back the information in which he was found? i only get 0 because it was only located 1 item, so the Dice will always be 0 if I use this excerpt

this.array.filter(o => o.bestOffer).map(function (o, i) { return i }))

that is, he found what I’m looking for but there is only 1 so the return will be 0, but in the case the array contains 4 items and the one I’m looking for is at position 1 or is the second of the list, how to get the number 1 because that’s where it is?

  array: [
    { bestOffer: false, title: planosControle[0].friendly },
    { bestOffer: true,  title: planosControle[1].friendly },
    { bestOffer: false, title: planosControle[1].friendly },
    { bestOffer: false, title: planosControle[2].friendly }
  ],

at the time, I found the index and used this form this.array.findIndex(o => o.bestOffer) and seems to have worked

  • at the moment, I found the index and used this form this.array.findIndex(o => o.bestOffer) and it seems to have worked

  • The index() will return only the index of the first element that meets the callback criterion. If you always have only 1 element that you look for, no problem to use. However, if this is not the case, you will have problem. The return always equal to 0 you are having is because your map() is being made in a new array returned by filter(), which is just the array with the element you are looking for.

1 answer

0


The return always equal to 0 you are having is because of your map() is being done in a new array returned by filter(), which is only an array containing the elements that met the callback criterion, in the case of the example only one element.

The indexOf() will return only the index of the first element that meets the callback criterion. Case always have only 1 element you seek, no problem to use. But if this is not the case and you want to find the index of one or more elements of the same array, you will have a problem.

One solution would be to implement a new function in the Array prototype, inspired by the findIndex() that already exists, like the code below:

Array.prototype.findAllIndex = function(predicate) {
    if (this === null) {
      throw new TypeError('Array.prototype.findIndex called on null or undefined');
    }
    if (typeof predicate !== 'function') {
      throw new TypeError('predicate must be a function');
    }
    var list = Object(this);
    var length = list.length >>> 0;
    var thisArg = arguments[1];
    var value;
    var indexs = [];

    for (var i = 0; i < length; i++) {
      value = list[i];
      if (predicate.call(thisArg, value, i, list)) {
        indexs.push(i);
      }
    }
    return indexs.length > 0 ? indexs : -1;
  };

This new function of the array will return an array of all the index elements compatible with your criterion in the callback or retorna -1 if you do not find any compatible elements.

You can call this new function as follows:

this.array.findAllIndex(o => o.bestOffer);

The findIndex() also only returns the index of the first element compatible with the callback criterion, not feasible for its use if you want to return the index of more than one element in the array.

I hope it’s clear and it helps you.

  • very good, I really only noticed the new array depis, when reading more times I noticed that the map creates a new array so it brought only 0 because it only had 1 element as explained, in fact the index worked because only 1 element had received the flag, as it was, if 2 elements receive the flag ai would have to evolve the function, very obtainable

Browser other questions tagged

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