How to check if a value exists in an object using JavaScript

In this tutorial, let’s look at the different ways to check if a value exists in an object using JavaScript. We’re going to be looking at a combination of straight-forward (directly searching through the list of values) and roundabout methods (searching via the list of properties).

Using Object.values

One of the easiest ways to check if a value (not a property) exists in an object is by using a combination of the Object.values and includes methods.

The Object.values method creates an array of all the values in the object, as shown below:

const person = {
  name: 'John',
  age: 15,
  eyeColor: 'Blue',
  hairColor: 'Blond',
  gpa: 3.5
}

console.log(Object.values(person)); //['John',15,'Blue','Blond',3.5] 

Now that you have an array with all the values present, you can do anything to this to find the value we need. There are several array methods that’ll help you find an array element.

One of the easiest ways to find if our value exists in this ‘array of object values’ is by using the ‘includes’ method. This method returns true if the value exists, and false if it doesn’t.

function doesValueExist(obj,value) {
  if(Object.values(obj).includes('John')) { 
    return 'Value exists';
  }

  else { 
    return 'Value does not exist';
  }
}

const person = {
  name: 'John',
  age: 15,
  eyeColor: 'Blue',
  hairColor: 'Blond',
  gpa: 3.5
}

console.log(doesValueExist(person,'John')); //'Value exists' 

You can also use the indexOf method. This method returns the index of the value (in an array) if it exists, and -1 if it doesn’t exist.

So, if the returned index is of value 0 or higher, our value exists in the array, (that is, the object the array was created from), as shown below:

if(Object.values(obj).indexOf(value) >= 0) 

Note: You can also manually iterate through the array using the ‘for’, ‘for in’ or ‘forEach’ loops.

The object.entries method

There’s also the ‘entries’ method that returns a 2-dimensional array, where every inner array contains a string value pertaining to the property and the corresponding value.

console.log(Object.entries(person)); // [['name','John'], ['age',15], ['eyeColor','blue'], ['hairColor','Blond'], ['gpa',3.5]] 

As you can see above, when we print the result of Object.entries on the ‘person’ object, we get arrays within an array, where the first array contains the property ‘name’ and its corresponding value ‘John’ and so on.

Now, we can just loop through this array, where every iteration will get us a 2-element array. We can get those 2 elements separately, as shown in the snippet below:

function doesValueExist(obj,value) {
  for(const [key,val] of Object.entries(obj)) {
    if(val === value) return 'Value exists';
    else return 'Value does not exist';
  }
} 

As you can see above, by using the ‘for of’ loop, we can loop through the 2-dimensional array, retrieve the 2 elements as [key,val] pairs, and then just access ‘val’ in every iteration.

Iterating through the object with the ‘for in’ loop

There’s also the manual way of doing things. Why don’t we just use the ‘for in’ loop to loop through the object.

Every iteration of this loop will access every property in the object, in the order it was created. Now, we can manually check if objName[propName], which is the value corresponding to that property, is the same as the value we’re looking for, as shown below:

function doesValueExist(obj,value) {
  for(const o in obj) {
    if(obj[o] === value) return 'Value exists';
    else return 'Value does not exist';
  }
} 

Using the ‘for of’ loop with pre-defined Object methods

As you know, the ‘for of’ method is great for handling arrays and objects that result from pre-defined Object prototype methods. Why don’t we look at some more of them?

Object.keys

When you use the Object.keys method on an object, you’ll get back a list of all the enumerable keys in the object. This list will be stored in the form of an array and the keys will be stored in the form of strings.

Look at the example below:

console.log(Object.keys(person)); // ['name','age','eyeColor','hairColor','gpa']; 

Our object’s properties have become the elements of an array.

Now, you can use the ‘for of’ loop to loop through this array of properties, access their corresponding values during every iteration, and check if the value is the same as the one we’re looking for, as shown below:

function doesValueExist(obj,value) {
  for(const p of Object.keys(obj)) {
    if(obj[p] === value) return 'Value exists';
    else return 'Value does not exist';
  }
}

const person = {
  name: 'John',
  age: 15,
  eyeColor: 'Blue',
  hairColor: 'Blond',
  gpa: 3.5
}

console.log(doesValueExist(person,'Susan')); //'Value dooes not exist'
console.log(doesValueExist(person,'John')); //'Value exists' 

Object.getOwnPropertyNames method

Just like the Object.keys method, you can use the Objet.getOwnPropertyNames method to achieve the same result.

The only difference between ‘keys’ and ‘getOwnPropertyNames’ is that the first method returns only enumerable properties, while the second returns both enumerable and non-enumerable properties.

Look at the example below:

console.log(Object.getOwnPropertyNames(person)); // ['name','age','eyeColor','hairColor','gpa']; 

As you can see above, we’re getting back the same list of elements because all our properties are enumerable in this example.

So, the difference between these two methods will not affect the result of our current example, so you can use either method.

for(const p of Object.getOwnPropertyNames(obj)) { 

Reflect.ownKeys

The Reflect.ownKeys method works like the Object.keys method. It returns all the keys (properties) of an object, but only its own properties and none of the ones inherited from the prototype.

But it also returns the non-enumerable and symbol properties, which doesn’t make any difference in our current example, as shown below:

console.log(Reflect.ownKeys(person)); // ['name','age','eyeColor','hairColor','gpa']; 

That’s it! We’ve looked at multiple ways of checking if a value exists in an object, including methods that involve manual looping and pre-defined JavaScript functions.

Leave a Comment