Sometimes, we want to check if a value exists in an object using JavaScript.
In this article, we’ll look at how to check if a value exists in an object using JavaScript.
How to check if a value exists in an object using JavaScript?
To check if a value exists in an object using JavaScript, we use the Object.values and the array includes method.
For instance, we write
const exists = Object.values(obj).includes("test1");
to get an array of property values in object obj with Object.values.
Then we call includes with the value we’re looking for in obj.
Conclusion
To check if a value exists in an object using JavaScript, we use the Object.values and the array includes method.