While working in javascript arrays, often there is a requirement to check if all the elements of the array are equal. This article demonstrates easy ways to check if all array elements are equal using different methods and example illustrations.
Table of Contents:
- Check if all elements of array are equal using every()
- Check if all elements of array are equal using reduce()
- Check if all elements of array are equal using Set
- Check if all elements of array are equal using filter()
- Check if all elements of array are equal using iteration
Check if an object is an array using every()
Javascript’s every() method will test if all elements pass the test implemented by the provided function. If all the elements pass the test, true is returned. Else false is returned.
Example:-
Check if the values in the below arrays are equal.
Frequently Asked:
- myArray1 = [“Javascript”, “Javascript”, “Javascript”, “Javascript”]
- myArray2 = [“Javascript”,1,”Javascript”]
- myArray3
Code:-
function checkArrayEqualElements(_array) { if(typeof _array !== 'undefined') { var firstElement = _array[0]; return _array.every(function(element) { return element === firstElement; }); } return "Array is Undefined" ; } //usage let myArray1 = ["Javascript", "Javascript", "Javascript", "Javascript"]; let myArray2 = ["Javascript",1,"Javascript"]; let myArray3 ; console.log(checkArrayEqualElements(myArray1)); console.log(checkArrayEqualElements(myArray2)); console.log(checkArrayEqualElements(myArray3));
Output:-
true false Array is Undefined
Explanation:-
- There is an if statement within the function checkArrayEqualElements(_array) to check if the array is undefined. Only if the array is not undefined, every() method is applied to each element of the array.
- In the every() method, it is checked that all array elements are equal to the first element. If all of them pass the given condition, true is returned else, false is retuned.
- If the array is undefined, “Array is Undefined” is returned.
Check if an object is an array using reduce()
Javascript’s reduce() method will execute a callback function called reducer on each element of the array that passes the return value from the preceding calculation. The result after applying the reducer on all elements of the array is a single value.Â
Example:-
Check if the values in the below arrays are equal.
- myArray1 = [“Javascript”, “Javascript”, “Javascript”, “Javascript”]
- myArray2 = [“Javascript”,1,”Javascript”]
- myArray3
Code:-
function checkArrayEqualElements(_array) { if(typeof _array !== 'undefined') { return !!_array.reduce(function(a, b){ return (a === b) ? a : NaN; }); } return "Array is Undefined"; } //usage let myArray1 = ["Javascript", "Javascript", "Javascript", "Javascript"]; let myArray2 = ["Javascript",1,"Javascript"]; let myArray3 ; console.log(checkArrayEqualElements(myArray1)); console.log(checkArrayEqualElements(myArray2)); console.log(checkArrayEqualElements(myArray3));
Output:-
true false Array is Undefined
Explanation:-
- There is an if statement within the function checkArrayEqualElements(_array) to check if the array is undefined. Only if the array is not undefined, reduce() method is applied to the array.
- In the reduce() method, a callback function is applied on each element of the array that compares two elements. If they are the same, one of the array element is passed on for the next comparison. Else NaN is returned.
- If the array is undefined, “Array is Undefined” is returned.
Check if an object is an array using Set
Javascript’s Set enables saving unique values.
Javascript’s Set.prototype.has() method is used to find out if an element with a particular value exists in the SET or not.
Example:-
Check if the values in the below arrays are equal.
- myArray1 = [“Javascript”, “Javascript”, “Javascript”, “Javascript”]
- myArray2 = [“Javascript”,1,”Javascript”]
- myArray3
Code:-
function checkArrayEqualElements(_array) { if(typeof _array !== 'undefined') { return new Set(_array).size == 1; } return "Array is Undefined"; } //usage let myArray1 = ["Javascript", "Javascript", "Javascript", "Javascript"]; let myArray2 = ["Javascript",1,"Javascript"]; let myArray3 ; console.log(checkArrayEqualElements(myArray1)); console.log(checkArrayEqualElements(myArray2)); console.log(checkArrayEqualElements(myArray3));
Output:-
true false Array is Undefined
Explanation:-
- There is an if statement within the function checkArrayEqualElements(_array) to check if the array is undefined. Only if the array is not undefined, the array elements are added to the Set.
- If the size of the Set is equal to 1, true is returned. Else false is returned.
- If the array is undefined, “Array is Undefined” is returned.
Check if an object is an array using filter()
Javascript’s filter() method will return a new array consisting of all the elements that pass the test implemented by the provide function.
Example:-
Check if the values in the below arrays are equal.
- myArray1 = [“Javascript”, “Javascript”, “Javascript”, “Javascript”]
- myArray2 = [“Javascript”,1,”Javascript”]
- myArray3
Code:-
function checkArrayEqualElements(_array) { if(typeof _array !== 'undefined') { return _array.filter((e , i ,a)=> e===a[0]).length === _array.length; } return "Array is Undefined"; } //usage let myArray1 = ["Javascript", "Javascript", "Javascript", "Javascript"]; let myArray2 = ["Javascript",1,"Javascript"]; let myArray3 ; console.log(checkArrayEqualElements(myArray1)); console.log(checkArrayEqualElements(myArray2)); console.log(checkArrayEqualElements(myArray3));
Output:-
true false Array is Undefined
Explanation:-
- There is an if statement within the function checkArrayEqualElements(_array) to check if the array is undefined. Only if the array is not undefined, filter() is applied to all elements of the array.
- In the filter() method, each element of the array is compared with the first element of the array. The array elements found equal to the first element are added to a new array.
- Finally, if the length of the new array equals the length of the original array, true is returned. Else false is returned.
- If the array is undefined, “Array is Undefined” is returned.
Check if an object is an array using iteration()
Example:-
Check if the values in the below arrays are equal.
- myArray1 = [“Javascript”, “Javascript”, “Javascript”, “Javascript”]
- myArray2 = [“Javascript”,1,”Javascript”]
- myArray3
Code:-
function checkArrayEqualElements(_array) { if(typeof _array !== 'undefined') { for (var i=1; i< _array.length; i++) { if (_array[i] !== _array[0]) { return false; } else { return true; } } } return "Array is Undefined"; } //usage let myArray1 = ["Javascript", "Javascript", "Javascript", "Javascript"]; let myArray2 = ["Javascript",1,"Javascript"]; let myArray3 ; console.log(checkArrayEqualElements(myArray1)); console.log(checkArrayEqualElements(myArray2)); console.log(checkArrayEqualElements(myArray3));
Output:-
true false Array is Undefined
Explanation:-
- There is an if statement within the function checkArrayEqualElements(_array) to check if the array is undefined.
- If the array is not undefined, a loop is run to check all the array elements are equal to the first element of the array. If anyone of the element is not equal to the first element, false is returned. Else true is returned.
- If the array is undefined, “Array is Undefined” is returned.
Read More:
- Javascript: Insert an item to array at specific index
- Javascript: Check if an array is empty
- Javascript: Convert array to string (4 ways)
I hope this article helped you to check if all the elements of the array are equal. Good Luck !!!