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 ;-)

2
  • This isn't a duplicate per se, as I'm having a similar problem where the key is numerical and not a string. Commented May 26, 2019 at 9:15
  • @WayneSmallman there are no numeric keys. The key in obj = { 1: "hello" } is converted to a string. There is no difference between obj[1] and obj["1"], both refer to exactly the same property. Since numeric keys are just coerced to numbers, there is no special handling for those. Nothing beyond JavaScript property access: dot notation vs. brackets? is needed - obj.1 is not valid, so it has to use bracket notation. However, that's not intrinsically tied to searching for objects, just regular thing in all property access. Commented Dec 12, 2023 at 11:30

5 Answers 5

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)

Sign up to request clarification or add additional context in comments.

1 Comment

this should be the best answer.
3

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

or

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

or

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

Comments

1

if(users.find(({ name }) => name === "Mike")) {
    // do your thing
}

Comments

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'))

Comments

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);
    }
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.