3

I have an array of objects like this:

"users": [
   {
     "type": "User",
     "userId": "5b774905c2b2ac0f33ac4cc7",
     "name": "Mike"
   },
   {
     "type": "User",
     "userId": "5b77490f3084460f2986bd25",
     "name": "Pater"
   }
]

Now, I would like to check if my array contains an object with the name "Mike".

How do I go about this in javascript?

I am thinking something like this, but not sure how:

if ( "Mike" in users ) {
   // Do something...
}

Hoping for help on this, and thanks in advance ;-)

CodeBoyCode
  • 2,227
  • 12
  • 29
Mansa
  • 2,277
  • 10
  • 37
  • 67

5 Answers5

15

There are many ways to implement that, for example, here are four ES6 methods for that:

  1. some() will return true or false, depending on the condition. It tests, does at list one element fits the condition
  2. find() will return an item itself (the first matched item), if the condition evaluates to true, and undefined if it evaluates to false.
  3. findIndex() will return an index of the item (the first matched index), if the condition evaluates to true, and -1 if it evaluates to false
  4. filter() will create a new array with all items, which fit the condition (otherwise it returnes an empty array)

const users = [
   {
     "type": "User",
     "userId": "5b774905c2b2ac0f33ac4cc7",
     "name": "Mike"
   },
   {
     "type": "User",
     "userId": "5b77490f3084460f2986bd25",
     "name": "Pater"
   }
];

const someObject = users.some(item => item.name === 'Mike');
const targetObject = users.find(item => item.name === 'Mike');
const targetIndex = users.findIndex(item => item.name === 'Mike');
const filteredObjects = users.filter(item => item.name === 'Mike');

console.log('someObject:', someObject)
console.log('targetObject:', targetObject)
console.log('targetIndex:', targetIndex)
console.log('filteredObjects:', filteredObjects)
P.S.
  • 15,970
  • 14
  • 62
  • 86
3

users.find(user => user.name ==='Mike')

or

users.filter(user => user.name ==='Mike').length !== 0

or

users.map(user => user.name).includes('Mike')

ashish singh
  • 6,526
  • 2
  • 15
  • 35
1

if(users.find(({ name }) => name === "Mike")) {
    // do your thing
}
Juan Elfers
  • 770
  • 7
  • 13
1

Some is the job for the task. It returns true or false based on if the value exists according to the callback.

let users = [
   {
     "type": "User",
     "userId": "5b774905c2b2ac0f33ac4cc7",
     "name": "Mike"
   },
   {
     "type": "User",
     "userId": "5b77490f3084460f2986bd25",
     "name": "Pater"
   }
]


console.log(users.some(i => i.name == 'Mike'))
console.log(users.some(i => i.name == 'Joe'))
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
0

You need to check the value of the property name of the inner objects.

This proposal checks every object, because it is not granted, that name is unique.

var users = [{ type: "User", userId: "5b774905c2b2ac0f33ac4cc7", name: "Mike" }, { type: "User", userId: "5b77490f3084460f2986bd25", name: "Pater" }];

users.forEach(o => {
    if (o.name === 'Mike') {
        console.log(o);
    }
});
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392