avatar
check if element exists in array using includes method Javascript

• How can we accomplish this if we have a JavaScript array and need to determine whether a particular element is present and retrieve it?

const flowerLists = [
  {
    name: "Rose",
    color: "Red"
  },
  {
    name: "Sunflower",
    loc: "Yellow",
  }
];

• Using `some` to retrieve object and or check exists of object in Javascript array.

const hasRose = flowerLists.some(flower => flower.name === 'Rose');

if (hasRose) {
  const index = flowerLists.findIndex(flower => flower.name === 'Rose');
  flowerLists.splice(index, 1);
}

Note: You can use `includes` replace `some` by following syntax:

const hasRose = flowerLists.some(flower => flower.name.includes('Rose'));
You need to login to do this manipulation!