方法一:
var ary = new Array("111","22","33","111")
var s = ary.join(",")+","
for(var i=0i<ary.lengthi++) {
if(s.replace(ary[i]+",","").indexOf(ary[i]+",")>-1) {
alert("数组中有重复元素:" + ary[i])
break
}
}
方法二:
var ary = new Array("111","22","33","111")
var nary=ary.sort()
for(var i=0i<ary.lengthi++){
if (nary[i]==nary[i+1]){
alert("数组重复内容:"+nary[i])
}
}
方法三:
function isRepeat(arr){
var hash = {}
for(var i in arr) {
if(hash[arr[i]])
return true
hash[arr[i]] = true
}
return false
}
思路:将数组里的最大值和最小值都取出来,然后判断最大值和最小值是否想等。
声明一个简单数组:
const tempArr = [7,6,5,4,3,2,1,]
Math.max.apply(null,tempArr) == Math.min.apply(null,tempArr)
声明一个对象数组:
const obj = [{id:1,price:1.5},{id:2,price:1.5,}]
const arr = obj.map(o =>o.price)
Math.max.apply(null,arr) == Math.min.apply(null,arr)