1.对数组对象,根据某个对象计算重复的个数
例如:
getData() { let arrData = [{ ‘a‘: ‘你‘, ‘b‘: ‘1‘, ‘c‘: ‘他‘ }, { ‘a‘: ‘他‘, ‘b‘: ‘2‘, ‘c‘: "它" }, { ‘a‘: ‘你‘, ‘b‘: ‘3‘, ‘c‘: "它" }, { ‘a‘: ‘他‘, ‘b‘: ‘4‘, ‘c‘: "它" }, { ‘a‘: ‘它‘, ‘b‘: ‘5‘, ‘c‘: "它" }] this.getWordCnt(arr) },
计算函数
// 根据等级数据计算重复的个数 getWordCnt (arr) { let arrData = arr let obj = {} for (let i = 0; i < arrData.length; i++) { var item = arrData[i].a // a为计算的属性,可换成b,c obj[item] = (obj[item] + 1) || 1 } console.log(obj) return obj },
打印结果
原文地址:https://www.cnblogs.com/wgl0126/p/11596956.html
时间: 2024-11-13 14:35:42