close

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

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How to check if a value exists in an object using JavaScript in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

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

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

    to check if a value exists in an object using JavaScript You can use the Array method .some:
    var exists = Object.keys(obj).some(function(k) { return obj[k] === "test1"; });

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

    to check if a value exists in an object using JavaScript You can use the Array method .some:
    var exists = Object.keys(obj).some(function(k) { return obj[k] === "test1"; });

Method 1

You can turn the values of an Object into an array and test that a string is present. It assumes that the Object is not nested and the string is an exact match:

var obj = { a: 'test1', b: 'test2' };
if (Object.values(obj).indexOf('test1') > -1) {
   console.log('has test1');
}

Method 2

You can use the Array method .some:

var exists = Object.keys(obj).some(function(k) {
    return obj[k] === "test1";
});

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment