I have a scenario where I need to calculate the average values of specific keys in an array:
const users = [
{ name: 'Adam', age: 20, country: 'France', weight: 100 },
{ name: 'Adam', age: 28, country: 'Germany', weight: 100 },
{ name: 'Adam', age: 28, country: 'India', weight: 200 },
{ name: 'Adam', age: 40, country: 'France', weight: 200 },
{ name: 'Oliver', age: 28, country: 'France', weight: 200 }
];
The averaging should be done based on the keys 'name' and 'country' for the keys 'age' and 'weight'
output = [
{ name: 'Adam', age: 30, country: 'France', weight: 150 },
{ name: 'Adam', age: 28, country: 'Germany', weight: 100 },
{ name: 'Adam', age: 28, country: 'India', weight: 200 },
{ name: 'Oliver', age: 28, country: 'France', weight: 200 }
];
If certain keys are not defined, they should be removed during the calculation process:
In this case, averaging is based solely on the key 'name' for 'age' and 'weight'
output = [
{ name: 'Adam', age: 29, weight: 150 },
{ name: 'Oliver', age: 28, weight: 200 }
];