10万json数组中,如何高性能的统计出每个字段出现的次数

数据:

data:[{class: "4.1",num: 2,area: "李沧区",school: "青岛四流中路中路第三小学"},{class: "4.2",num: 31,area: "李沧区",school: "青岛四流中路中"},{class: "1",num: 12,area: "李渡区",school: "李渡区学校"},

{class: "21",num: 1,area: "城阳区",school: "城阳区学校"}]

最终期盼数据:

this.result = `选择区域${numQY}个,学校${numXX}所,班级${numBJ}个,用户${user}名`;


回答:

reduce 方法。

let data = [{class: "4.1",num: 2,area: "李沧区",school: "青岛四流中路中路第三小学"},{class: "4.2",num: 31,area: "李沧区",school: "青岛四流中路中"},{class: "1",num: 12,area: "李渡区",school: "李渡区学校"},

{class: "21",num: 1,area: "城阳区",school: "城阳区学校"}];

var total = data.reduce((acc,cur)=>(acc.areaSet.add(cur.area),acc.schoolSet.add(cur.school),acc.classSet.add(cur.class),(acc.userTotal+=cur.num),acc),{areaSet:new Set(),schoolSet:new Set(),classSet:new Set(),userTotal:0});

console.log(total.areaSet.size,total.schoolSet.size,total.classSet.size,total.userTotal);

// 3 4 4 46

forEach 方法。

let data = [{class: "4.1",num: 2,area: "李沧区",school: "青岛四流中路中路第三小学"},{class: "4.2",num: 31,area: "李沧区",school: "青岛四流中路中"},{class: "1",num: 12,area: "李渡区",school: "李渡区学校"},

{class: "21",num: 1,area: "城阳区",school: "城阳区学校"}];

let areaSet = new Set();

let schoolSet = new Set();

let classSet = new Set();

let userTotal = 0;

data.forEach(item=>(areaSet.add(item.area),schoolSet.add(item.school),classSet.add(item.class),(userTotal+=item.num)));

console.log(areaSet.size,schoolSet.size,classSet.size,userTotal);

10万json数组中,如何高性能的统计出每个字段出现的次数

3 4 4 46

以上是 10万json数组中,如何高性能的统计出每个字段出现的次数 的全部内容, 来源链接: utcz.com/p/936020.html

回到顶部