let arr = [ {name:'商品一',sort:'10011',unit:'吨',count:'5',price:'2.70' ,money:'13.50'}, {name:'商品二',sort:'20011',unit:'吨',count:'5',price:'0.85' ,money:'4.25'}, {name:'商品一',sort:'10011',unit:'吨',count:'5',price:'2.70' ,money:'13.50'}, {name:'商品三',sort:'20051',unit:'吨',count:'5',price:'0.40' ,money:'2.00'}, {name:'商品二',sort:'20011',unit:'吨',count:'5',price:'0.85' ,money:'4.25'}, ]
function add(arr) { arr = arr.reduce((obj, item) => { let find = obj?.find(e => e.name == item.name && e.sort == item.sort && e.unit == item.unit && e.price == item.price) let _d = { name: item.name, money: item.money, sort: item.sort, unit: item.unit, count: item.count, price: item.price } find ? (find.money= Number(find.money) + Number(item.money),find.count= Number(find.count) + Number(item.count)) : obj?.push(_d) return obj }, []) return arr }
arr1 = [1,2,3,1,4,5,6]
let l = arr1.filter(e => e!= 1)
console.log('arr1', l, l[0]=456)
let list = add(arr) console.log(list)