How to Find an Item in an Object in JavaScript?

Estimated read time 1 min read

You can use the Object.values() method in JavaScript to get all the values of an object and then use the Array.prototype.includes() method to find an item in the values.

Here’s an example that demonstrates how to find an item in an object:

const person = { name: "Jim", age: 35, city: "London" };

const values = Object.values(person);
const result = values.includes(35);

console.log(result);
/*
Output:
true
*/

In this example, the Object.values() method is used to get all the values of the person object, which are stored in the values array. The Array.prototype.includes() method is then used to check if the item 35 is present in the values array. If the item is found, the includes() method returns true. If the item is not found, the includes() method returns false.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply