Check if object has value
JavaScript, Object · Apr 10, 2023

Checks if the target value exists in a JSON object.
- Use
Object.values()
to get all the values of the object. - Use
Array.prototype.includes()
to check if the target value is included in the values array.
const hasValue = (obj, value) => Object.values(obj).includes(value);
const obj = { a: 100, b: 200 };
hasValue(obj, 100); // true
hasValue(obj, 999); // false
Written by Angelos Chalaris
I'm Angelos Chalaris, a JavaScript software engineer, based in Athens, Greece. The best snippets from my coding adventures are published here to help others learn to code.
If you want to keep in touch, follow me on GitHub.