How to find the array index with a value?

501,585

Solution 1

You can use indexOf:

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

You will get -1 if it cannot find a value in the array.

Solution 2

For objects array use map with indexOf:

var imageList = [
   {value: 100},
   {value: 200},
   {value: 300},
   {value: 400},
   {value: 500}
];

var index = imageList.map(function (img) { return img.value; }).indexOf(200);

console.log(index);

In modern browsers you can use findIndex:

var imageList = [
   {value: 100},
   {value: 200},
   {value: 300},
   {value: 400},
   {value: 500}
];

var index = imageList.findIndex(img => img.value === 200);

console.log(index);

Its part of ES6 and supported by Chrome, FF, Safari and Edge

Solution 3

Use jQuery's function jQuery.inArray

jQuery.inArray( value, array [, fromIndex ] )
(or) $.inArray( value, array [, fromIndex ] )

Solution 4

Here is an another way find value index in complex array in javascript. Hope help somebody indeed. Let us assume we have a JavaScript array as following,

var studentsArray =
     [
    {
    "rollnumber": 1,
    "name": "dj",
    "subject": "physics"
   },
   {
   "rollnumber": 2,
  "name": "tanmay",
  "subject": "biology"
   },
  {
   "rollnumber": 3,
   "name": "amit",
   "subject": "chemistry"
   },
  ];

Now if we have a requirement to select a particular object in the array. Let us assume that we want to find index of student with name Tanmay.

We can do that by iterating through the array and comparing value at the given key.

function functiontofindIndexByKeyValue(arraytosearch, key, valuetosearch) {

    for (var i = 0; i < arraytosearch.length; i++) {

    if (arraytosearch[i][key] == valuetosearch) {
    return i;
    }
    }
    return null;
    }

You can use the function to find index of a particular element as below,

var index = functiontofindIndexByKeyValue(studentsArray, "name", "tanmay");
alert(index);

Solution 5

Use indexOf

imageList.indexOf(200)
Share:
501,585

Related videos on Youtube

joedborg
Author by

joedborg

Updated on February 18, 2022

Comments

  • joedborg
    joedborg over 2 years

    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.

  • voigtan
    voigtan over 12 years
    you can use developer.mozilla.org/en/JavaScript/Reference/Global_Objects‌​/… or any Javascript framework to fix that.
  • joedborg
    joedborg over 12 years
    Have just found it won't work in IE8. What's my alternative?
  • joedborg
    joedborg over 12 years
    @voigtan How do I add this to my JS?
  • joedborg
    joedborg over 12 years
    I've done it, thanks. IE will one day catch up with modern times!
  • aruno
    aruno about 11 years
    according to this page developer.mozilla.org/en-US/docs/JavaScript/Reference/… you need Internet Explorer 9 for this feature (I almost wrongly assumed we were just talking IE 6 here)
  • Manse
    Manse about 11 years
    @Simon_Weaver on that page you linked there is a polyfill for all browsers using JavaScript 1.6 and up
  • pelms
    pelms over 10 years
    Also won't work in recent IE if in compatibility mode
  • James Blackburn
    James Blackburn over 10 years
    Was going to use the code marked as the correct answer but it seems that that code wont work in all versions of IE. This answer works in all versions of IE but I changed the code slightly: var arrayPosition = $.inArray( value, Array ); works perfectly
  • James Blackburn
    James Blackburn over 10 years
    var arrayPosition = $.inArray( value, Array ); will work in all versions of IE
  • Andrew
    Andrew over 8 years
    jQuery ≠ Javascript
  • Bindrid
    Bindrid over 6 years
    Note that findIndex is not implemented in IE 11
  • Jaqen H'ghar
    Jaqen H'ghar over 6 years
    @Bindrid it is written in the answer see "Its part of ES6 and supported by Chrome, FF, Safari and (Unfortunately only) IE edge"
  • moto
    moto over 4 years
    In addition to this only telling you if it exists, instead of getting the index/item, be cognizant of the compatibility (i.e. no IE): caniuse.com/#search=includes
  • Heretic Monkey
    Heretic Monkey almost 4 years