let arr = [
[
{ name: 'Apple', total: '5', id: '1'},
{ name: 'Banana', total: '11', id: '22'},
],
[
{ name: 'Apple', total: '1', id: '1'},
{ name: 'Banana', total: '15', id: '22'},
],
[
{ name: 'Apple', total: '5', id: '1'},
{ name: 'Banana', total: '10', id: '22'},
],
]
let result = Object.values(arr.flat().reduce((acc, item)=>{
if(!acc[item.name]){
acc[item.name] = {...item};
}else{
acc[item.name].total = `${+acc[item.name].total + +item.total}`;
}
return acc;
},{}));
console.log(result)
const km = [25, 45, 95, 125, 150, 200, 325, 250, 165, 350]
const cof = [1, 1.5, 1, 1.5, 1.2, 1.5, 1 , 0.5, 1.2, 1.5];
// как то так
const res = cof.reduce((a,v,i)=>(a[v]=(a[v]||0)+km[i],a),{});
console.log(res); // { 1: 445, "1.5": 720, "1.2": 315, "0.5": 250 }
console.log(Object.values(res)); // [445, 720, 315, 250];
var arr1 = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]];
var arr2 = [[10, 20, 30, 40], [50, 60, 70, 80], [90, 100, 110, 120]];
var arr3 = [[100, 200, 300, 400], [500, 600, 700, 800], [900, 1000, 1100, 1200]];
function summ_array(...arrs){
return arrs.reduce((acc,arr)=>{
if( Array.isArray(arr))
arr.forEach((subarr, i)=>{
if(!Array.isArray(acc[i]))
acc[i] = []
if( Array.isArray(subarr))
subarr.forEach((v, j)=>{
if(!acc[i][j])
acc[i][j] = 0;
acc[i][j]+=v;
});
});
return acc;
},[]);
}
var summ = summ_array(arr1, arr2, arr3);
console.log(summ);