JavaScript - Check if All Array Values Are Equal
Here are the different methods to check if all values of an array are equal or not in JavaScript
1. Using Array.every() method
First, get the array of elements. Pass it to an arrow function, which calls every() method on each array element and returns true if each element matches the first element of the array.
let a = [1, 1, 1, 1];
const all =
a => a.every(v => v === a[0]);
console.log(all(a));
let a = [1, 1, 1, 1];
const all =
a => a.every(v => v === a[0]);
console.log(all(a));
Output
true
In this example
- The all() function checks if all elements in the array are equal to the first element using the every() method.
- In this case, it returns true because all elements in [1, 1, 1, 1] are equal to the first element (1)
2. Using Array.reduce() method
First, get the array of elements. Pass it to a function called the reduce() method on the array element. Return true if each element matches the first element of the array.
let a = ["G", "F", "I", "J"];
function all(a) {
if (!a.length) return true;
return a.reduce(function (a, b) {
return (a === b) ? a : (!b);
}) === a[0];
}
console.log(all(a));
let a = ["G", "F", "I", "J"];
function all(a) {
if (!a.length) return true;
return a.reduce(function (a, b) {
return (a === b) ? a : (!b);
}) === a[0];
}
console.log(all(a));
Output
false
In this example:
- The all() function uses reduce() to compare all elements of the array a with each other and checks if they are all equal.
- It returns false because the elements in a (["G", "F", "I", "J"]) are not all equal.
3. Using Set
To check if all elements in an array are equal, we can pass the array to the Set constructor. Since a Set only stores unique elements, if the size of the set is 1, it indicates that all elements in the array are equal.
let a = [1, 1, 1, 1];
function all(a) {
const res = new Set(a).size === 1;
return res;
}
console.log(all(a));
let a = [1, 1, 1, 1];
function all(a) {
const res = new Set(a).size === 1;
return res;
}
console.log(all(a));
Output
true
In this example
- The all() function converts the array into a Set, which removes duplicate elements, and checks if the set's size is 1.
- It returns true because all elements in the array [1, 1, 1, 1] are the same, so the set contains only one unique element.
4. Using for...of loop
The for...of loop iterates over the iterable objects (like Array, Map, Set, arguments object, …,etc) and will check the element is same as the first element.
const a1 = [1, 1, 1, 1];
const a2 = [1, 1, 2];
function all(a) {
let eq = true;
for (const element of a) {
if (element !== a[0]) {
eq = false;
break;
}
}
return eq;
}
console.log(all(a1));
console.log(all(a2));
const a1 = [1, 1, 1, 1];
const a2 = [1, 1, 2];
function all(a) {
let eq = true;
for (const element of a) {
if (element !== a[0]) {
eq = false;
break;
}
}
return eq;
}
console.log(all(a1));
console.log(all(a2));
Output
true false
In this example
- The all() function checks if all elements in the array a are equal to the first element.
- It loops through each element in a and compares it with the first element (a[0]).
- If any element is different, it sets eq to false and breaks the loop.
- The function returns true if all elements are the same and false otherwise.
5. Using Array.some() Method
The Array.some() method to determine if any element in the array does not match the first element. If the method returns false, it means all elements are equal.
function elem(a) {
if (a.length === 0) return true;
const b = a[0];
return !a.some(element => element !== b);
}
const a1 = [1, 1, 1, 1];
const a2 = [1, 2, 1, 1];
console.log(elem(a1));
console.log(elem(a2));
function elem(a) {
if (a.length === 0) return true;
const b = a[0];
return !a.some(element => element !== b);
}
const a1 = [1, 1, 1, 1];
const a2 = [1, 2, 1, 1];
console.log(elem(a1));
console.log(elem(a2));
Output
true false
In this example
- The elem() function checks if all elements in the array arr are equal to the first element by using some() to find any element not equal to the first one.
- It returns true for arr1 (all elements are equal) and false for arr2 (not all elements are equal).