Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

People also ask

How do you check if a property is present in an object?

The first way is to invoke object. hasOwnProperty(propName) . The method returns true if the propName exists inside object , and false otherwise. hasOwnProperty() searches only within the own properties of the object.

How do you check if an object has a key value?

Use the in operator to check if a key exists in an object, e.g. "key" in myObject . The in operator will return true if the key is present in the object, otherwise false is returned. Copied! The syntax used with the in operator is: string in object .


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

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values


You can use the Array method .some:

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

Try:

var obj = {
   "a": "test1",
   "b": "test2"
};

Object.keys(obj).forEach(function(key) {
  if (obj[key] == 'test1') {
    alert('exists');
  }
});

Or

var obj = {
   "a": "test1",
   "b": "test2"
};

var found = Object.keys(obj).filter(function(key) {
  return obj[key] === 'test1';
});

if (found.length) {
   alert('exists');
}

This will not work for NaN and -0 for those values. You can use (instead of ===) what is new in ECMAScript 6:

 Object.is(obj[key], value);

With modern browsers you can also use:

var obj = {
   "a": "test1",
   "b": "test2"
};

if (Object.values(obj).includes('test1')) {
  alert('exists');
}

Shortest ES6+ one liner:

let exists = Object.values(obj).includes("test1");

Use a for...in loop:

for (let k in obj) {
    if (obj[k] === "test1") {
        return true;
    }
}

You can use Object.values():

The Object.values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

and then use the indexOf() method:

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

For example:

Object.values(obj).indexOf("test`") >= 0

A more verbose example is below:

var obj = {
  "a": "test1",
  "b": "test2"
}


console.log(Object.values(obj).indexOf("test1")); // 0
console.log(Object.values(obj).indexOf("test2")); // 1

console.log(Object.values(obj).indexOf("test1") >= 0); // true
console.log(Object.values(obj).indexOf("test2") >= 0); // true 

console.log(Object.values(obj).indexOf("test10")); // -1
console.log(Object.values(obj).indexOf("test10") >= 0); // false

For a one-liner, I would say:

exist = Object.values(obj).includes("test1");
console.log(exist);