There is an array in which there are objects with an array of data.

You need to get the sum of all elements of the array of objects and add each sum to the object, then you need to get the sum of two objects and also add to the object.

You also need to add the elements from the array of the first object with the elements from the array of the second email and add them to the array with the sum of each property, i.e. a) [1,2,3] and object b) [4,5,6] and add them get the output [5,7,9]

I was able to get the sum of two elements of the array, but the variant turned out to be very wooden, because it is tied to the property of the first object

function chartSumm(arr) { let obj, firstArr = [], lastArr = [], sumArray = [] arr.forEach(function(item, index) { for (let el in item.data) { if (item.label === 'Эккономия Π½Π° аннуляции') { firstArr = item.data; } else { lastArr = item.data } } }); let firstObjSum = firstArr.reduce(function(prev, curr, index) { return prev + curr }); let twoObjSum = lastArr.reduce(function(prev, curr, index) { return prev + curr }); obj = { 'Π¦Π΅Π½Π° Ρƒ стойки': firstObjSum, 'Аннуляция Π±Π΅Π· ΡˆΡ‚Ρ€Π°Ρ„Π°': twoObjSum, 'ΠžΠ±Ρ‰Π°Ρ экономия': firstObjSum + twoObjSum }; console.log(obj) } chartSumm([{ label: 'Эккономия Π½Π° аннуляции', backgroundColor: '#ef9d4d', data: [1000, 400, 1500, 1500, 3000, 4000] }, { label: 'Экономия Π½Π° Ρ†Π΅Π½Π΅ Ρƒ стойки', backgroundColor: '#476e8f', data: [1170, 460, 2000, 2500, 2700, 4000] }, ]) 

  • since you have different fields in the input objects and on weekends, in any case you will have to link them somehow. - Grundy
  • @Grundy Well, I just think there is suddenly a more universal option, because if you add another object, 2 arrays will fly to the second array already) - Drop
  • course there is, if you leave the same label - Grundy
  • @Grundy Well, yes, there was also an idea to immediately access the required array without the first cycles, but this is already over the top) but the moment when the first cycle ends I don’t know how else to determine without data binding) - Drop
  • What is the meaning of for (let el in item.data) ? You don’t do anything with this loop, just insert item.data 6 times in firstArr and lastArr . - Dmitry

1 answer 1

Since the labels are different, in any case they will have to register. To make the function more universal (and not for only 2 objects), you can take the result label to the object. I also took the label ΠžΠ±Ρ‰Π°Ρ Экономия separately from the function, but you can also set it inside.

 function chartSumm(arr, sumLabel, elementSumLabel) { var result = {}; result[sumLabel] = 0; result[elementSumLabel] = [0, 0, 0, 0, 0, 0] arr.forEach(function(item, index) { result[item.resultLabel] = 0; for (var i = 0; i < item.data.length; i++) { result[elementSumLabel][i] += item.data[i]; result[item.resultLabel] += item.data[i] } result[sumLabel] += result[item.resultLabel]; }); return result; } var info = chartSumm([{ label: 'Эккономия Π½Π° аннуляции', resultLabel: 'Π¦Π΅Π½Π° Ρƒ стойки', backgroundColor: '#ef9d4d', data: [1000, 400, 1500, 1500, 3000, 4000] }, { label: 'Экономия Π½Π° Ρ†Π΅Π½Π΅ Ρƒ стойки', resultLabel: 'Аннуляция Π±Π΅Π· ΡˆΡ‚Ρ€Π°Ρ„Π°', backgroundColor: '#476e8f', data: [1170, 460, 2000, 2500, 2700, 4000] }, ], 'ΠžΠ±Ρ‰Π°Ρ экономия', 'Π‘ΡƒΠΌΠΌΠ° элСмСнтов'); console.log(info); 

  • and how to be with an array of the totals of elements? Π’Π°ΠΊ ΠΆΠ΅ Π½ΡƒΠΆΠ½ΠΎ ΡΠ»ΠΎΠΆΠΈΡ‚ΡŒ элСмСнты ΠΈΠ· массива ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° с элСмСнтами ΠΈΠ· массива Π²Ρ‚ΠΎΡ€ΠΎΠ³ΠΎ эл ΠΈ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ Π² массив с суммой ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ свойства Ρ‚.Π΅ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ Π°) [1,2,3] ΠΈ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ Π±) [4,5,6] ΡΠ»ΠΎΠΆΠΈΡ‚ΡŒ ΠΈΡ… ΠΈ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ Π½Π° Π²Ρ‹Ρ…ΠΎΠ΄Π΅ [ 5,7,9] - Drop
  • @Drop updated the answer - Jurij Jazdanov