0

There are a few loops in javascript including forEach, some, map, and reduce. However, from what I currently understand, none of these are appropriate if you want to iterate through an array and return the value of a particular index. It seems like I am pretty much left with the standard for loop only. Is that true?

So for instance, if I have an array of objects... and I would like to find the index of the item with a particular value... could I use anything other than the regular for loop?

Grateful
  • 9,685
  • 10
  • 45
  • 77
  • 2
    [`Array.prototype.findIndex()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex) – Andreas Feb 21 '17 at 06:16
  • 1
    `findIndex()` or `indexOf()` – Mohit Bhardwaj Feb 21 '17 at 06:18
  • but what about if it is an array of objects? Let's say `[{name: 'foo', age: 23}, {name: 'foo2', age: 34}]`? How do I use indexOf to search this? – Grateful Feb 21 '17 at 06:20
  • 1
    @Grateful: `yourList.findIndex(function(item) { return item.name === 'foo2'; })`. – Tom Feb 21 '17 at 06:26
  • If it's an array of objects, even then you can use `findIndex()`. This functions takes a function in which you can define your equality criteria. Similarly, you can use a `forEach( item, index, array )` loop and check each element for equality. Whichever item matches, you can return that element's `index`. – Mohit Bhardwaj Feb 21 '17 at 06:26
  • @MohitBhardwaj forEach can't return anything from within.... are you sure about this approach? – Grateful Feb 21 '17 at 06:30
  • You're right in that `forEach` can't return anything using `return` statement. What I meant was you could declare a variable before the `forEach` loop and inside `forEach` you could update that variable to reflect the matched index. If it does not look clean, you can use `findIndex()`. – Mohit Bhardwaj Feb 21 '17 at 06:36
  • Okay... I like the `filterIndex` approach. @MohitBhardwaj, can you please post that as an official answer, so that I may accept it? :) – Grateful Feb 21 '17 at 06:41

5 Answers5

0

There is also the for ... of ...:

for (variable of iterable) {
  statement
}
J. P. Petersen
  • 4,871
  • 4
  • 33
  • 33
0

You can use a JavaScript library http://underscorejs.org. It have plenty of functions. for your purpose you can use _.indexOf . it will eturns the index at which value can be found in the array

example:

var data=[1, 2, 3];
var index= _.indexOf(data, 2);

out put will be 1

Ritz
  • 394
  • 1
  • 3
  • 12
0

Array.prototype.findIndex() , as suggested by @Andreas in comments.

You can pass in a function to findIndex() method and define your equality criteria in that function.

It will return the index of first array element, that satisfies the equality criteria defined in your function.

Mohit Bhardwaj
  • 9,650
  • 3
  • 37
  • 64
0

Array.prototype.findIndex() or Array.prototype.indexOf() like everyone said.

Bigdragon
  • 352
  • 1
  • 4
  • 16
-1

You could use foreach() rather than regular for.It is the simplest and you can iterate and get the index. For more info: JavaScript Array forEach() Method

Malcolm Vaz
  • 131
  • 5
  • I don't think you got my question.... I already mentioned the existence of forEach... however, the problem is that I can't get it to return the item value. – Grateful Feb 21 '17 at 06:23
  • Return the index of a particular element which is equal to the value, right? If you check the link I sent it returns both the index and value of the elements. – Malcolm Vaz Feb 21 '17 at 06:25
  • The link shows how you can get the items index and their value ...using a function with parameters. Sorry if I have confused this up. All the best. – Malcolm Vaz Feb 21 '17 at 06:29
  • The point is you can't say `result = array.forEach(..)` because the `.forEach()` function itself doesn't return a result. You'd have to say `result = "something"` inside the loop, and there is no way to break out of a `forEach()` so it's inefficient if you are just trying to find one particular element. – nnnnnn Feb 21 '17 at 06:31
  • Thank you for your time @MalcolmVaz. Although, I didn't find what I was looking for exactly... it was very kind of you to try. – Grateful Feb 21 '17 at 06:34