0

I have an array of objects where I need to find the sum of each array of values. it looks like this

 '1664625533491050': [
    380.942, 394.71, 387.936, 273.902,
     137.58, 133.82,  66.471,  58.616,
     45.966, 36.849,  49.002,  32.275,
     65.209, 34.679,  60.806,  46.576,
     132.89, 62.005,   0.386,    0.09,
      0.005,  0.601,   0.004,   0.316,
      0.227,  0.185,   0.206,   0.142,
        0.1,  0.121,   0.089,   0.016,
      0.033,  0.008,       0,   0.002,
      0.001
  ],
  '1664625594588444': [
    381.931, 394.71, 387.936, 283.902,
    146.365, 133.82,  66.471,  58.616,
     45.966, 36.849,  49.002,  32.275,
     64.953, 34.679,  60.806,  46.576,
    132.889, 68.005,   0.386,    0.09,
      0.005,  0.601,   0.004,   0.316,
      0.227,  0.185,   0.206,   0.142,
        0.1,  0.121,   0.089,   0.016,
      0.033,  0.008,       0,   0.002,
      0.001
  ]
}

for each object I need to find the sum of each array. I believe that I need to map and reduce each one but I am unsure how to reference each array

function getSum(obj) {
      for (const key in obj) {
        const firstStep = obj[key];
        const secondStep = firstStep.map(x => x/*not sure how to reference*/);
        const thirdStep = secondStep.reduce((a, b) => a + b);
        obj[key] = thirdStep
  
        return obj
      }

How can I reference the array so that I can get the sum of each objects array?

example

'1664625533491050': 2345.789,
'1664625594588444': 1789.587
// those aren't the actual totals just example of what Im looking to get
4
  • those arrays are not "unnamed" - they are properties of the object, 1664625533491050 and 1664625594588444 - what is it you're trying to do in firstStep.map ??? note, map callback gets 3 arguments, (currentElement, currentIndex, wholeArray) - so, perhaps the third argument is what you want? though, looking at it again, it looks like you don't need your the map step at all, you just need to the reduce Commented Oct 15, 2022 at 4:22
  • is it perhaps the return obj inside the for loop that is short circuiting the loop so only one property is processed? Is that the actual issue? There's no need to return the incoming obj since your code mutates the incoming obj Commented Oct 15, 2022 at 4:29
  • Yea that was it. I removed map and instead just reduced then moved return obj outside of the loop and it's working fine. thanks!! Commented Oct 15, 2022 at 4:43
  • You do understand that you are mutating the incoming object, of course Commented Oct 15, 2022 at 4:49

4 Answers 4

2

Not sure what you're attempting in the .map - it's not needed at all

Look at this code without that step

Note: your code was doing a return inside the for loop, so, only one element of the object was processed;

Note 2: this mutates the passed in object - as you can see from the console.log(input)

const input = {
    '1664625533491050': [
        380.942, 394.71, 387.936, 273.902,
        137.58, 133.82,  66.471,  58.616,
        45.966, 36.849,  49.002,  32.275,
        65.209, 34.679,  60.806,  46.576,
        132.89, 62.005,   0.386,    0.09,
        0.005,  0.601,   0.004,   0.316,
        0.227,  0.185,   0.206,   0.142,
        0.1,  0.121,   0.089,   0.016,
        0.033,  0.008,       0,   0.002,
        0.001
    ],
    '1664625594588444': [
        381.931, 394.71, 387.936, 283.902,
        146.365, 133.82,  66.471,  58.616,
        45.966, 36.849,  49.002,  32.275,
        64.953, 34.679,  60.806,  46.576,
        132.889, 68.005,   0.386,    0.09,
        0.005,  0.601,   0.004,   0.316,
        0.227,  0.185,   0.206,   0.142,
        0.1,  0.121,   0.089,   0.016,
        0.033,  0.008,       0,   0.002,
        0.001
    ]
};
function getSum(obj) {
    for (const key in obj) {
        const array = obj[key];
        const sum = array.reduce((a, b) => a + b);
        obj[key] = sum;
    }
    return obj;
}
getSum(input);
console.log(input);

If you do not want to mutate the passed in object, then the code is simply

const input = {
    '1664625533491050': [
        380.942, 394.71, 387.936, 273.902,
        137.58, 133.82,  66.471,  58.616,
        45.966, 36.849,  49.002,  32.275,
        65.209, 34.679,  60.806,  46.576,
        132.89, 62.005,   0.386,    0.09,
        0.005,  0.601,   0.004,   0.316,
        0.227,  0.185,   0.206,   0.142,
        0.1,  0.121,   0.089,   0.016,
        0.033,  0.008,       0,   0.002,
        0.001
    ],
    '1664625594588444': [
        381.931, 394.71, 387.936, 283.902,
        146.365, 133.82,  66.471,  58.616,
        45.966, 36.849,  49.002,  32.275,
        64.953, 34.679,  60.806,  46.576,
        132.889, 68.005,   0.386,    0.09,
        0.005,  0.601,   0.004,   0.316,
        0.227,  0.185,   0.206,   0.142,
        0.1,  0.121,   0.089,   0.016,
        0.033,  0.008,       0,   0.002,
        0.001
    ]
};

function betterGetSum(obj) {
    return Object.fromEntries(Object.entries(obj).map(([key, array]) => [key, array.reduce((a, b) => a + b)]));
}
console.log(betterGetSum(input));

Sign up to request clarification or add additional context in comments.

3 Comments

I like the one-liner
@Nick - both codes could be one-liners - the first code is minimal changes to OP's code to show it works (with the return moved to outside the loop of course)
@Nick me too,too
1

You can use reduce to do it

let data={
'1664625533491050': [
    380.942, 394.71, 387.936, 273.902,
     137.58, 133.82,  66.471,  58.616,
     45.966, 36.849,  49.002,  32.275,
     65.209, 34.679,  60.806,  46.576,
     132.89, 62.005,   0.386,    0.09,
      0.005,  0.601,   0.004,   0.316,
      0.227,  0.185,   0.206,   0.142,
        0.1,  0.121,   0.089,   0.016,
      0.033,  0.008,       0,   0.002,
      0.001
  ],
  '1664625594588444': [
    381.931, 394.71, 387.936, 283.902,
    146.365, 133.82,  66.471,  58.616,
     45.966, 36.849,  49.002,  32.275,
     64.953, 34.679,  60.806,  46.576,
    132.889, 68.005,   0.386,    0.09,
      0.005,  0.601,   0.004,   0.316,
      0.227,  0.185,   0.206,   0.142,
        0.1,  0.121,   0.089,   0.016,
      0.033,  0.008,       0,   0.002,
      0.001
  ]
}

let keys = Object.keys(data)
let result = {}
keys.forEach(k => {
  let value = data[k].reduce((a,v) => a+ v,0)
  result[k]=value
})

console.log(result)

Comments

0

You can simply achieve this by using Array.reduce() method.

Live Demo :

const obj = {
  '1664625533491050': [
    380.942, 394.71, 387.936, 273.902,
    137.58, 133.82,  66.471,  58.616,
    45.966, 36.849,  49.002,  32.275,
    65.209, 34.679,  60.806,  46.576,
    132.89, 62.005,   0.386,    0.09,
    0.005,  0.601,   0.004,   0.316,
    0.227,  0.185,   0.206,   0.142,
    0.1,  0.121,   0.089,   0.016,
    0.033,  0.008,       0,   0.002,
    0.001
  ],
  '1664625594588444': [
    381.931, 394.71, 387.936, 283.902,
    146.365, 133.82,  66.471,  58.616,
    45.966, 36.849,  49.002,  32.275,
    64.953, 34.679,  60.806,  46.576,
    132.889, 68.005,   0.386,    0.09,
    0.005,  0.601,   0.004,   0.316,
    0.227,  0.185,   0.206,   0.142,
    0.1,  0.121,   0.089,   0.016,
    0.033,  0.008,       0,   0.002,
    0.001
  ]
};

Object.keys(obj).forEach(key => {
  obj[key] = obj[key].reduce((sum, elem) => sum + elem);
})

console.log(obj);

Comments

-1

You can use, for in. Look

const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
  console.log(`${property}: ${object[property]}`);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.