Home > Net >  how to map an array of objects where array is unnamed
how to map an array of objects where array is unnamed

Time:10-15

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

CodePudding user response:

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)

CodePudding user response:

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));

CodePudding user response:

You can use, for in. Look

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

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