0

I have an array of objects that I want to iterate through and sum some values if the keys exist in the sumList array and average all others. How can we best achieve this?

let sumList = ['TEST1', 'TEST2']

data.forEach(item => {
    Object.keys(item).forEach(key => {
        if (!result.hasOwnProperty(key)) {
            result[key] = 0;
        }
        if(sumList.includes(key)) {
            result[key] += item[key]
        } else {
            result[key] = (result[key] + item[key]) / 2;
        }
    });
});

The bug i am running into, i think has to do with the fact that initially value is 0... and it tried to divide 0 + the next value by 2. I would like to do this in the same loop insteam of first summing them then running another loop and averaging them.

2 Answers 2

1

You can use reduce() for the sum, filter() for the count, then use those two values to calculate the average:

const getValidKey = obj => sumList.find(key=>obj.hasOwnProperty(key));

const sum = data.reduce((accum, currentObj) => {
    const key = getValidKey(currentObj);
    return key ? accum + currentObj[key] : accum;
}, 0);

const count = data.filter(currentObj=> 
    getValidKey(currentObj) !== undefined
).length;

const average = sum/count;

Live Demo

There is nothing wrong with iterating through an array twice, as long as there aren't thousands of items. Code readability is more important than improving performance by a few milliseconds.

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

1 Comment

with this... it gives the average of one value... I want average per key item in the array
1

Should be like:

let sumList = ['TEST1', 'TEST2'], o, n, z, v, results = [];
for(let a of data){
  o = {sums:0}; n = 0; z = 0;
  for(let o of a){
    for(let i in o){
      v = o[i];
      if(sumList.indexOf(i) === -1){
        o.sums += v;
      }
      else{
        z++; n += v;
      }
    }
  }
  o.avgs = z === 0 ? 0 : n/z;
  results.push(o);
}

1 Comment

Not all people are robots. Some explanation for the OP and other visitors would be great in addition to your code!

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.