Js statistics the number of repetitions of elements in the array (b)

let list = [
  {name: "1000 Points", the serialNumber:. 6 },
  {name: "500 Points", the serialNumber:. 7 },
  {name: "50 Points", the serialNumber:. 8 },   {name: "50 Points", the serialNumber:. 8 },   {name: "50 Points", the serialNumber:. 8 }];

How to list an array into

[ 
  {Title: "1000 Points", num: 1},
  {Title: "500 Points", num: 1},
  {Title: "50 Points", num: 3}
] 
// NUM: number of repetitions of the same elements serialNumber

 

let _res = [];
for(let i = 0; i < list.length;) {
let count = 0;
for(let j = i; j < list.length; j++) {
if(list[i].serialNumber == list[j].serialNumber) {
count++;
}
}
let obj = {
title:list[i].name,
num:count
}
_res.push(obj)
i += count;
}
console.log(_res)
//[{"title":"1000积分","num":1},{"title":"500积分","num":1},{"title":"50积分","num":3}]

Guess you like

Origin www.cnblogs.com/yixiancheng/p/12048722.html