find index by key in array of object js 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: find the index of an object in an array

const letters = [{letter: 'a'},{letter: 'b'},{letter: 'c'}]

const index = letters.findIndex((element, index) => {
  if (element.letter === 'b') {
    return true
  }
})

//index is `1`

Tags:

Misc Example