检查数组中的每个项目在javascript中是否相同 [英] Check if each item in an array is identical in javascript
本文介绍了检查数组中的每个项目在javascript中是否相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要测试数组中的每个项目是否彼此相同。例如:
I need to test whether each item in an array is identical to each other. For example:
var list = [l,r,b]
应评估为false,因为每个项目不相同。另一方面,这个:
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?
推荐答案
function identical(array) {
for(var i = 0; i < array.length - 1; i++) {
if(array[i] !== array[i+1]) {
return false;
}
}
return true;
}
这篇关于检查数组中的每个项目在javascript中是否相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文