0

Suppose to have an array of two or more objects as:

const arr =[{PM1:10, PM10:20},{PM1:20, PM10:30, CO:27}, {NO2:30}]

How can I efficiently compute average values for each property in the objects comprising the array? I don't know in advance the properties in each object and even if there are common properties. I would expect a result:

{PM1:15, PM10:25, CO:27, NO2:30}

I suppose that I can use reduce but I can't figure it out how.

1
  • so, what did you try ? Commented Oct 6, 2020 at 15:02

2 Answers 2

1

You can loop through the property names of an object using either for-in (which includes inherited properties) or an array from Object.keys (which doesn't). In both cases, only enumerable properties are included.

For instance:

const arr =[{PM1:10, PM10:20},{PM1:20, PM10:30, CO:27}, {NO2:30}];

const result = {};
const counts = new Map();
for (const element of arr) {
    for (const key in element) {
        if (Object.hasOwn(element, key)) {
            // Count this property
            counts.set(key, (counts.get(key) ?? 0) + 1);
            // Add it to the sum
            result[key] = (result[key] ?? 0) + element[key];
        }
    }
}
// Update with the averages
for (const [key, count] of counts) {
    result[key] = result[key] / counts.get(key);
}

Live Example:

// Quick-and-dirty polyfill for older environments without `Object.hasOwn`:
if (!Object.hasOwn) {
    Object.defineProperty(Object, "hasOwn", {
        value: Function.prototype.call.bind(Object.prototype.hasOwnProperty),
        writable: true,
        configurable: true,
    });
}

const arr =[{PM1:10, PM10:20},{PM1:20, PM10:30, CO:27}, {NO2:30}];

const result = {};
const counts = new Map();
for (const element of arr) {
    for (const key in element) {
        if (Object.hasOwn(element, key)) {
            // Count this property
            counts.set(key, (counts.get(key) ?? 0) + 1);
            // Add it to the sum
            result[key] = (result[key] ?? 0) + element[key];
        }
    }
}
// Update with the averages
for (const [key, count] of counts) {
    result[key] = result[key] / counts.get(key);
}

console.log(result);

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

2 Comments

This is very nice! Could you please show how we could avoid for-in altogether, if possible?
@Emman - You have to get the property names somehow. If it's not for-in, then it'll be Object.keys (as I mentioned) or similar (like Object.getOwnPropertyNames or Reflect.ownKeys). for-in is likely a good choice for this (although I might have included an own property check; I think I'll do that).
0

just 2 reduce...

const arr =
  [ { PM1:10, PM10:20}
  , { PM1:20, PM10:30, CO:27}
  , { NO2:30 }
  ]
const average =
  arr.reduce((a,c)=>
    {
    for(let key in c) 
      {
      let line = a.find( x=>x.k === key )
      if (!line) a.push( line = { k:key, sum:0, nb:0 } )
      line.sum += c[key]
      ++line.nb
      }
    return a
    },
  []).reduce((a,c)=>
    {
    a[c.k] = c.sum / c.nb
    return a
    },{})

console.log ( average )
.as-console-wrapper { max-height: 100% !important; top: 0; }

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.