Javascript indexOf method with multiple values

user5672329 picture user5672329 · Apr 14, 2016 · Viewed 27.8k times · Source

I have an array wich contains multiple same values

["test234", "test9495", "test234", "test93992", "test234"]

Here I want to get the index of every test234 in the array

For that I've tried Array.prototype.indexOf() method. But It only returns me 0 but I want it to return me [0, 2, 4].

How can I do that?

Answer

JoeL picture JoeL · Apr 14, 2016

Just make it a for loop to check each array element.

var array = ["test234", "test9495", "test234", "test93992", "test234"];

for (i=0;i<array.length;i++) {
  if (array[i] == "test234") {
    document.write(i + "<br>");
  }
}