I have an object in JavaScript:
var obj = { "a": "test1", "b": "test2" }
How do I check that test1 exists in the object as a value?
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
1.4m articles
1.4m replys
5 comments
56.9k users