user6913790
user6913790

Reputation:

Check if an array of objects exists

I have an array of objects:

items: [
  { name: "Cheese Puffs", price: 3 },
  { name: "Can of Soda", price: 1.75 }
];

I want to do something like items["cheesePuffs"] === true. But as it is in an array it won't work properly.

Upvotes: 2

Views: 187

Answers (7)

alaadine andolsi
alaadine andolsi

Reputation: 11

You can use find

let exist = myArrOfObjects.find(o => o.cheesePuffs === 'yes')

Upvotes: 1

Suhaib Janjua
Suhaib Janjua

Reputation: 3574

Try the following code. It will return you the object where name matches to 'Cheese Puffs'.

let items = [{
    name: "Cheese Puffs",
    price: 3
  },
  {
    name: "Can of Soda",
    price: 1.75
  }
];

let itemExist = items.find(x => x.name === 'Cheese Puffs');

if (itemExist) {
  console.log(itemExist);
} else {
  console.log("Item not Exist");
}

Upvotes: 1

GiuServ
GiuServ

Reputation: 1235

As suggestion, you can also decide to not use an array, but to use a json object, where the index of each item is the unique name of your objects (in the example "cheesePuffs" identifies "Cheese Puffs")

let items = {
"cheesePuffs": {name: "Cheese Puffs",price: 3},
"canOfSoda": {name: "Can of Soda",price: 1.75},
};

console.log("exist: ", items.cheesePuffs!== undefined)
console.log(items.cheesePuffs)
// can also access to item in this way:
console.log(items["cheesePuffs"])

console.log("not exist", items.noCheesePuffs!== undefined)
console.log(items.noCheesePuffs)

Upvotes: 1

Dupinder Singh
Dupinder Singh

Reputation: 7769

okay simple solutions

try this

const x = [{}];

if(x.find(el => el.cheesePuffs) == undefined)
  console.log("empty objects in array ")
  
const myArrOfObjects = [{
  cheesePuffs: "yes"
},
{
  time: "212"
}];


if(myArrOfObjects.find(el => el.cheesePuffs) == undefined)
  console.log("empty objects in array ")
  else
  console.log("objects available in array ")

Upvotes: 1

ANIK ISLAM SHOJIB
ANIK ISLAM SHOJIB

Reputation: 3248

First of all you have an array of objects so you can't simply use

myArrOfObjects["cheesePuffs"]

because array required an index so it should be myArrOfObjects[0]["cheesePuffs"]

let items = [
  { name: "Cheese Puffs", price: 3 },
  { name: "Can of Soda", price: 1.75 }
];


let filter = items.find( el => el.price === 3 );

console.log(filter);

another approach

  let items = [
      { name: "Cheese Puffs", price: 3 },
      { name: "Can of Soda", price: 1.75 }
    ];


    let filter = items.filter( el => el.price === 3 );

    console.log(filter);

Upvotes: 0

Code Maniac
Code Maniac

Reputation: 37755

You can use some and hasOwnProperty, If you need actual value instead of Boolean values you can use find instead of some

const myArrOfObjects = [{
  cheesePuffs: "yes"
},
{
  time: "212"
}];

let findByName = (name) => {
  return myArrOfObjects.some(obj=> {
    return obj.hasOwnProperty(name)
  })
}

console.log(findByName("cheesePuffs"))
console.log(findByName("time"))
console.log(findByName("123"))

Upvotes: 2

Brad
Brad

Reputation: 163240

What you want is Array.find().

myArrOfObjects.find(el => el.cheesePuffs);

Assuming the property you're looking for is truthy, this returns the element, {cheesePuffs: "yes"} which is truthy. If it weren't found, it would be undefined.

Upvotes: 3

Related Questions