Get index of an object in an array

PHOTO EMBED

Wed Mar 02 2022 18:29:37 GMT+0000 (Coordinated Universal Time)

Saved by @jen_magpantay

// Get the index of an obj by using its id as param
function teste(id) {
  // use findeIndex() to get the id of an array of objects that would match the param
  const index = data.findIndex((item) => item.id === id);
  // then, you can use this as base for statements
  id === data[index].id ? doSomething() : doOtherThing();
}
content_copyCOPY