javascript 检查数组中的每个项目在javascript中是否相同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9646943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if each item in an array is identical in javascript
提问by Nick
I need to test whether each item in an array is identical to each other. For example:
我需要测试数组中的每个项目是否彼此相同。例如:
var list = ["l","r","b"]
var list = ["l","r","b"]
Should evaluate as false, because each item is not identical. On the other hand this:
应评估为 false,因为每个项目都不相同。另一方面这个:
var list = ["b", "b", "b"]
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?
应该评估为真,因为它们都是相同的。实现这一目标的最有效(在速度/资源方面)方式是什么?
回答by pimvdb
In ES5, you could do:
在 ES5 中,你可以这样做:
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.
.every
也会短路。
回答by Dogbert
function identical(array) {
for(var i = 0; i < array.length - 1; i++) {
if(array[i] !== array[i+1]) {
return false;
}
}
return true;
}
回答by Jivings
function matchList(list) {
var listItem = list[0];
for (index in list) {
if(list[index] != listItem {
return false;
}
}
return true;
}
回答by Jen
You could always do a new Set, and check the length.
你总是可以做一个新的 Set,并检查长度。
var set1 = [...new Set(list)].length === 1;
回答by Izhar Ul Haque
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- begin snippet: js hide: false console: true babel: false -->
回答by Chang
let arr = ["l","r","b"];
The one line answer is:
单行答案是:
arr.every((val, ind, arr) => val === arr[0]);
arr.every((val, ind, arr) => val === arr[0]);
You can look into Array.everyfor more details.
您可以查看Array.every以获取更多详细信息。
Note:
笔记:
- Array.everyis available
ES5
onwards. - This method returns
true
for any condition put on an empty array. - Syntax:
arr.every(callback[, thisArg])
orarray.every(function(currentValue, index, arr), thisValue)
- It does not change the original array
- The execution of every() is short-circuited. As soon as every()finds an array element that doesn't match the predicate, it immediately returns false and doesn't iterate over the remaining elements
- Array.every
ES5
以后可用。 - 此方法返回
true
放置在空数组上的任何条件。 - 语法:
arr.every(callback[, thisArg])
或array.every(function(currentValue, index, arr), thisValue)
- 它不会改变原始数组
- every() 的执行是短路的。一旦every()找到与谓词不匹配的数组元素,它会立即返回false 并且不会迭代剩余元素
回答by Waynn Lue
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;
回答by Kory Sharp
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.
我的建议是删除重复项(查看在 JavaScript 数组中查找重复值的最简单方法),然后检查长度是否为 1。这意味着所有项目都相同。
回答by Erix
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;
}