Check whether a value exist in a JavaScript Object

In the last blog post, we see how to check whether a key exists in a JS Object. You can refer it here

In this article, we will see whether a value present in any of the values in an object.

Let's consider a JS object,

const author = {
  name: 'Param',
  age: 31,
  occupation: 'Software Developer'
};

Now, I want to check whether any of the values contain the value Param.

// Convert object values into an array
const authorValues = Object.values(author);
console.log(authorValues); // ["Param", 30, "Software Developer"]

// Check the values
console.log(authorValues.includes('Param')); // true

First, We converted all the object values into an array. Then, search the list for the value.

This technique only works for the direct objects, it won't work for nested objects. Also, this technique will work only for an object with primitive values. (i.e., string, integer, boolean, etc.,)

We will see a separate example to check for value match in a deeply nested object.