get index of an element in 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: js get index of item in array

array.indexOf("item");

Example 3: get index of element in array javascript

var scores = [10, 20, 30, 10, 40, 20];
console.log(scores.indexOf(30)); // 2
console.log(scores.indexOf(50)); // -1

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

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

Example 5: get item in array from index

var valueAtIndex1 = myValues[1];

Tags:

C Example