find the index of an element in an array javascript code example

Example 1: find index of object in array javascript

a = [
  {prop1:"abc",prop2:"qwe"},
  {prop1:"bnmb",prop2:"yutu"},
  {prop1:"zxvz",prop2:"qwrq"}];
    
index = a.findIndex(x => x.prop2 ==="yutu");

console.log(index);

Example 2: how to find the index of a value in an array in javascript

var list = ["apple","banana","orange"]

var index_of_apple = list.indexOf("apple") // 0

Example 3: js find index in list

let myList = ['foo', 'bar', 'qux'];

myList.indexOf('bar');	// 1

Example 4: index of value in array

// for dictionary case use findIndex 
var imageList = [
   {value: 100},
   {value: 200},
   {value: 300},
   {value: 400},
   {value: 500}
];
var index = imageList.findIndex(img => img.value === 200);

Example 5: js array get index

var fruits = ["Banana", "Orange", "Apple", "Mango"];
return fruits.indexOf("Apple"); // Returns 2

Example 6: how to get the index of an array in javascript

search = (arr, item) => { return arr.indexOf(item); }

Tags:

Php Example