Reputation: 345
I need to test whether each item in an array is identical to each other. For example:
var list = ["l","r","b"]
Should evaluate as false
, because each item is not identical. On the other hand this:
var list = ["b", "b", "b"]
Should evaluate as true
because they are all identical. What would be the most efficient (in speed/resources) way of achieving this?
Upvotes: 22
Views: 35852
Reputation: 420
To check if all values in an array are equal in JavaScript, you can use the every() method in combination with the === operator to compare each element of the array to the first element, like this:
function allEqual(arr) {
return arr.every(val => val === arr[0]);
}
This function takes an array arr as its parameter and returns true if all values in the array are equal, and false otherwise.
Here's an example usage:
const arr1 = [1, 1, 1, 1];
const arr2 = [1, 2, 3, 4];
const arr3 = ['foo', 'foo', 'foo'];
console.log(allEqual(arr1)); // true
console.log(allEqual(arr2)); // false
console.log(allEqual(arr3)); // true
In the example above, arr1 and arr3 contain only equal values, so the allEqual() function returns true for both of them. arr2 contains different values, so the function returns false.
Upvotes: 0
Reputation: 136
function identical(array) {
// a variable holding standard value
//against this standard value we are examining the array
var standard = array[1];
for (var i = 0; i < array.length; i++) {
if (array[i] !== standard) {
return false;
}
}
return true;
}
identical([1, 1, 1, 1, 1]); //return true
identical(['a', 'a', 'a']); //return true
identical(['a', 'a', 'b'])
function identical(array) {
// a variable holding standard value
//against this standard value we are examining the array
var standard = array[1];
for (var i = 0; i < array.length; i++) {
if (array[i] !== standard) {
return false;
}
}
return true;
}
identical([1, 1, 1, 1, 1]); //return true
identical(['a', 'a', 'a']); //return true
identical(['a', 'a', 'b'])
Upvotes: 1
Reputation: 818
arr.every(i=>i==arr[0]) //will return true if all items in arr are identical
Upvotes: 4
Reputation: 453
The one line answer is:
arr.every((val, ind, arr) => val === arr[0]);
You can look into Array.every for more details.
Note:
ES5
onwards.true
for any condition put on an empty array.arr.every(callback[, thisArg])
or array.every(function(currentValue, index, arr), thisValue)
Upvotes: 9
Reputation: 352
You could always do a new Set, and check the length.
var set1 = [...new Set(list)].length === 1;
Upvotes: 12
Reputation: 222168
function identical(array) {
for(var i = 0; i < array.length - 1; i++) {
if(array[i] !== array[i+1]) {
return false;
}
}
return true;
}
Upvotes: 17
Reputation: 154838
In ES5, you could do:
arr.every(function(v, i, a) {
// first item: nothing to compare with (and, single element arrays should return true)
// otherwise: compare current value to previous value
return i === 0 || v === a[i - 1];
});
.every
does short-circuit as well.
Upvotes: 19
Reputation: 7105
function allEqual(list)
{
if(list.length == 0 || list.length == 1)
{
return true;
}
for (index in list) {
if(list[index] != list[index+1] {
return false;
}
}
return true;
}
Upvotes: 0
Reputation: 490
My suggestion would be to remove duplicates (check out Easiest way to find duplicate values in a JavaScript array), and then check to see if the length == 1. That would mean that all items were the same.
Upvotes: 0
Reputation: 11375
var list = ["b", "b", "b"];
var checkItem = list[0];
var isSame = true;
for (var i = 0; i < list.length; i++) {
if (list[i] != checkItem) {
isSame = false;
break;
}
}
return isSame;
Upvotes: 1
Reputation: 23250
function matchList(list) {
var listItem = list[0];
for (index in list) {
if(list[index] != listItem {
return false;
}
}
return true;
}
Upvotes: 2