function times(arr){
var m=0,times=0//m是数组中的元素,times用来统计出现的次数
// for循环遍历arr数组
for(var i=0i<arr.lengthi++){
if(arr[i]==m){
times++//数组中有相同值就加1
}
}
return times
console.log(times)//这是打印出的出现的次数
}
times([0, 1, 2, 0, 1, 2])
var t = [{value:'待料停'},{value:'运行'},{value:'其他停'},{value:'离线'},{value:'断纱停'},{value:'关机'},{value:'关机'}]
for(var i of t){
var count = 0
for(var j of t){
if(j.value === i.value){
count++
}
}
i.count = count
}
console.log(t)
嫌循环太麻烦,也可以这样简化:
var t = [{value:'待料停'},{value:'运行'},{value:'其他停'},{value:'离线'},{value:'断纱停'},{value:'关机'},{value:'关机'}]
var res = Array.from(t,item =>{
item.count = t.filter(j =>j.value === item.value).length
return item
})
console.log(res)
let arr = [3, 2, {a: 1}, 1, {a: 1}, 1]
function ana(arr) {
let newArr = []
let unique = []
for (let i of arr) {
i = JSON.stringify(i)
//如果已有数值相同的元素存在
if (unique.includes(i)) {
newArr[unique.indexOf(i)].push(i)
} else {
let arr = []
arr.push(i)
unique.push(i)
newArr.push(arr)
}
}
return re(newArr)
}