How to find the array index with a value?

javascript find index of object in array by property
find index of element in array python
find index of element in array java
angularjs find index of object in array by id
find index of object in array angular 2
array find
findindex is not a function
find index of element in array? - matlab

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.

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.

JavaScript, indexOf() function is used to find the index of the first occurrence of the search this function is the searchElement which is the value to be searched in the array. How To Find The Index of Value in Numpy Array Python numpy.where() function iterates over a bool array, and for every True, it yields corresponding the element array x, and for every False, it yields corresponding item from array y. So, it returns an array of elements from x where the condition is True and elements from y elsewhere.

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);

Array.prototype.findIndex(), If it finds an array element where the function returns a true value, findIndex() returns the index of that array element (and does not check the remaining values)​  The starting index of the search. 0 (zero) is valid in an empty array. The index of the first occurrence of value, if it's found, within the range of elements in array that extends from startIndex to the last element; otherwise, the lower bound of the array minus 1.

Use jQuery's function jQuery.inArray

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

Array.prototype.indexOf(), Negative values will start at the given position counting from the end, and search to the end. Technical Details. Return Value: A Number, representing the position​  In this article we will discuss how to find index of a value in a Numpy array (both 1D & 2D) using numpy.where (). Let’s create a Numpy array from a list of numbers i.e. Now let’s see how to to search elements in this Numpy array. In the above numpy array element with value 15 occurs at different places let’s find all it’s indices i.e.

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);

JavaScript Array findIndex() Method, The object to locate in array . Returns. Int32. The index of the first occurrence of value in array , if found; otherwise, the  The second and third arguments to INDEX are the numbers 3 and 1, which instruct INDEX to find the value in the third row and first column of the array and return it to the worksheet. You can enter the full INDEX formula as just given normally, with an array of LINEST results as its first argument, without the Ctrl and Shift and Enter combination — that is, without array-entering it.

Use indexOf

imageList.indexOf(200)

JavaScript Array indexOf() Method, Function to find the index of an element in a primitive array in Java we can use Binary Search Algorithm to search the specified array for the specified value. The findIndex() method executes the callback function once for every index in the array until it finds the one where callback returns a truthy value (that is, a value that coerces to true). If such an element is found, findIndex() immediately returns the element's index.

Array.IndexOf Method (System), A closure that takes an element as its argument and returns a Boolean value that You can use the predicate to find an element of a type that doesn't conform to  The idx part will contain the column number of containing the max element of each row. You can use max() to get the max value. The max function can also return the index of the maximum value in the vector. To get this, assign the result of the call to max to a two element vector instead of just a single variable.

Find index of an element in given array in Java, Find index of a value in 1D Numpy array. In the above numpy array element with value 15 occurs at different places let's find all it's indices i.e.. For instance, you can examine the even elements in a matrix, find the location of all 0s in a multidimensional array, or replace NaN values in data. You can perform these tasks using a combination of the relational and logical operators. The relational operators ( >, <, >=, <=, ==,

firstIndex(where:), The FindIndex<T> (T [], Int32, Predicate<T>) method overload is used to search the array beginning at position 2 and continuing to the end of the array. It finds the element at position 5. Finally, the FindIndex<T> (T [], Int32, Int32, Predicate<T>) method overload is used to search the range of three elements beginning at position 2. It

Comments
  • Excellent answer here -> stackoverflow.com/questions/237104/…
  • developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…
  • you can use developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… or any Javascript framework to fix that.
  • Have just found it won't work in IE8. What's my alternative?
  • @voigtan How do I add this to my JS?
  • I've done it, thanks. IE will one day catch up with modern times!
  • Also won't work in recent IE if in compatibility mode
  • Note that findIndex is not implemented in IE 11
  • @Bindrid it is written in the answer see "Its part of ES6 and supported by Chrome, FF, Safari and (Unfortunately only) IE edge"
  • 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
  • jQuery ≠ Javascript
  • 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)
  • @Simon_Weaver on that page you linked there is a polyfill for all browsers using JavaScript 1.6 and up
  • 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