snippetjavascriptTip
Check if all values of a JavaScript array are equal
Viewed 0 times
javascriptarecheckequalallarrayvalues
Problem
Checking if all array elements match a condition in JavaScript is pretty simple. But how can you check if all values of an array are equal? It's just a matter of finding the right value to compare the rest of the array to.
Using
For more complex values, such as objects, you might want to use a mapping function to compare the elements. This way, you can compare the elements based on a specific property or a custom comparison function.
The technique is the same as before, except that you call the mapping function on the first element and then compare the rest of the array to the result.
Using
Array.prototype.every() is what comes to mind and it's the right path to go down. As we need to compare all elements to each other, we can just use the first element as the reference and compare the rest of the array to it.For more complex values, such as objects, you might want to use a mapping function to compare the elements. This way, you can compare the elements based on a specific property or a custom comparison function.
The technique is the same as before, except that you call the mapping function on the first element and then compare the rest of the array to the result.
Solution
const allEqual = arr => arr.every(val => val === arr[0]);
allEqual([1, 1, 1]); // true
allEqual([1, 1, 2]); // falseFor more complex values, such as objects, you might want to use a mapping function to compare the elements. This way, you can compare the elements based on a specific property or a custom comparison function.
The technique is the same as before, except that you call the mapping function on the first element and then compare the rest of the array to the result.
Code Snippets
const allEqual = arr => arr.every(val => val === arr[0]);
allEqual([1, 1, 1]); // true
allEqual([1, 1, 2]); // falseconst allEqualBy = (arr, fn) =>
arr.every((val, i) => fn(val, i, arr) === fn(arr[0], 0, arr));
allEqualBy([{ a: 1 }, { a: 1 }, { a: 1 }], obj => obj.a); // true
allEqualBy([{ a: 1 }, { a: 1 }, { a: 2 }], obj => obj.a); // falseContext
From 30-seconds-of-code: all-values-of-array-are-equal
Revisions (0)
No revisions yet.