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
– Roberto Monteiro
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.
– Lucas Ayrosa