Merge the same data in the js array and return the number of data

// 根据相同数据合并
			getWordCnt(arr,key) {
				var newArr = [...new Set(arr.map(i => i[key]))]; // 去重的时候需要注意和普通数组不同
				var list = [];
				newArr.forEach(item => {
					list.push(arr.filter(t => t[key] === item));
				})
				var mlist = [];
				list.forEach((item, index) => {
					let countAdd = item.reduce((total, g) => {
						return total + Number(g.qty)
					}, 0);
					mlist.push({
						...item[0],
						qty: countAdd
					})
				});
				return mlist
			}

Guess you like

Origin blog.csdn.net/QQ_Empire/article/details/126032585