jdborg jdborg - 4 months ago 17
Javascript Question

JavaScript find array index with value

Say I've got this

imageList = [100,200,300,400,500];


Which gives me

[0]100 [1]200
etc...

Is there any way in javascript to return the index with the value?

I.e. I want the index for 200, I get returned 1.

Hope I've explained it well enough.

Answer

you can use indexOf:

var imageList = [100,200,300,400,500];
var index = imageList.indexOf(200); // 1

you will get -1 if it cant find a value in the array.

Comments