标签:ber else style console col pre 个数 == OLE
原数组:
var arr = [ { time: 1, title: ‘2‘ }, { time: 1, title: ‘2‘ }, { time: 2, title: ‘1‘ } ]
最终数组:
let arrResult = [ { time: 1, number: 2, title: "2 2" }, { time: 2, number: 1, title: "1"} ]
实现代码:
let arrResult = [] arr1 = arr.forEach(item => { let flag = arrResult.find(item1 => item1.time === item.time) if (!flag) { arrResult.push({ time: item.time, number: 1, title: item.title, }) } else { flag.number++ flag.title = `${flag.title} ${item.title}` } }) console.log(arrResult)
js将数组对象中某个值相同的对象合并成一个新对象并把这个对象插入到数组当中,增加相同对象的个数
标签:ber else style console col pre 个数 == OLE
原文地址:https://www.cnblogs.com/yihengbaobei/p/12933528.html