Check if all Values in Object are Equal in JavaScript

avatar

Borislav Hadzhiev

Sun Nov 28 20213 min read

banner

Photo by Max Nguyen

Check if all Values in Object are Equal #

To check if all of the values in an object are equal, use the Object.values() method to get an array of the object's values and convert the array to a Set. If the Set contains a single element, then all of the values in the object are equal.

index.js
function allAreEqual(obj) { return new Set(Object.values(obj)).size === 1; } console.log(allAreEqual({num1: 1, num2: 1})); // ๐Ÿ‘‰๏ธ true console.log(allAreEqual({num1: 10, num2: 20})); // ๐Ÿ‘‰๏ธ false

We created a reusable function that checks if all of the values in an object are equal.

The first step is to use the Object.values method to get an array of the object's values.

index.js
// ๐Ÿ‘‡๏ธ [1, 1] console.log(Object.values({num1: 1, num2: 1}));

Then, we use the Set() constructor to convert the array of values into a Set object.

Set objects only store unique values, so none of the duplicates are added to theSet.

We access the size property on the Set and check if the Set contains a single element.

If the Set only contains 1 element, then all of the values of the object are equal.

An alternative approach is to use the Array.every method on the object's values.

To check if all of the values in an object are equal, use the Object.values() method to get an array of the object's values and call the every method on the array. On each iteration, check if the current value is equal to the value at index 0 of the array and return the result.

index.js
function allAreEqual(obj) { return Object.values(obj).every((value, _index, arr) => { if (value === arr[0]) { return true; } return false; }); } console.log(allAreEqual({num1: 1, num2: 1})); // ๐Ÿ‘‰๏ธ true console.log(allAreEqual({num1: 10, num2: 20})); // ๐Ÿ‘‰๏ธ false

The function we passed to the every() method gets called for each element in the array until it returns a falsy value or iterates over the entire array.

If the function returns a falsy value, the every method short-circuits and returns false.

The falsy values in JavaScript are: false, null, undefined, 0, "" (empty string), NaN (not a number). All other values are truthy.

In the example, we check if the current value is equal to the element at position 0. If the condition is met for all values, we can conclude that they are all equal.

I prefer using the Set approach as it does most of the work for us by automatically removing the duplicates.

However, the every() method allows us to write more logic and potentially handle some edge cases.

For example, the Set conversion approach would return false when passed an empty object. This is because, we check if the Set has exactly 1 element.

The every method on the other hand, returns true for any condition if supplied an empty array.

index.js
// ๐Ÿ‘‡๏ธ true console.log([].every(value => value === value + 1));

This is something to be aware of. If the object is empty, the every approach would just return true and consider all values in the object to be equal.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee