Check if array contains a matching object with array.some()

PHOTO EMBED

Tue Apr 26 2022 10:58:27 GMT+0000 (Coordinated Universal Time)

Saved by @lancerunsite #javascript #js #array #object #some

// Supported in IE 9-11
const people = [
  {id: 1, name: 'John'},
  {id: 2, name: 'Adam'},
];

const isFound = people.some(element => {
  if (element.id === 1) {
    return true;
  }

  return false;
});

console.log(isFound); // 👉️ true

if (isFound) {
  // object is contained in array
}
content_copyCOPY