JavaScript:如何根据特定键/字段对json数组进行平均
JavaScript: How to average json array based on specific keys / fields
我想对特定键进行平均,例如
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 }
];
键'age'和'weight'根据键'name'和'country'
取平均值
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 }
];
删除未定义的键:
键 'age' 和 'weight' 根据键 'name'
取平均值
output = [
{ name: 'Adam', age: 29, weight: 150 },
{ name: 'Oliver', age: 28, weight: 200 }
];
您可以使用 filter、map 和 reduce 等函数来实现这些特定目标。
为确保键存在,您过滤掉缺少这些键的条目:
// Keep entries that have the age property
const cleanedUsers = users.filter(x => x.age)
为了得到用户的平均年龄,你可以将清理后的数组归约为一个数字,然后除以数组的长度。这可以在一行代码中完成。
const average = users.filter(x => x.age).reduce((acc,x) => acc + x.age, 0) / users.length
您可以按想要的键对其进行分组,并为每个组获取每个想要的 属性 的平均值。
function getAverages(array, groupKeys, averageKeys) {
var groups = {},
result = [];
array.forEach(o => {
var key = groupKeys.map(k => o[k]).join('|'),
group = groups[key];
if (!group) {
groups[key] = { count: 0, payload: {} };
group = groups[key];
averageKeys.forEach(k => group[k] = 0);
groupKeys.forEach(k => group.payload[k] = o[k]);
result.push(group.payload);
}
groups[key].count++;
averageKeys.forEach(k => group.payload[k] = (group[k] += o[k]) / group.count);
})
return result;
}
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 }];
console.log(getAverages(users, ['name', 'country'], ['age', 'weight']));
console.log(getAverages(users, ['name'], ['age', 'weight']));
.as-console-wrapper { max-height: 100% !important; top: 0; }
我想对特定键进行平均,例如
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 }
];
键'age'和'weight'根据键'name'和'country'
取平均值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 }
];
删除未定义的键:
键 'age' 和 'weight' 根据键 'name'
取平均值output = [
{ name: 'Adam', age: 29, weight: 150 },
{ name: 'Oliver', age: 28, weight: 200 }
];
您可以使用 filter、map 和 reduce 等函数来实现这些特定目标。
为确保键存在,您过滤掉缺少这些键的条目:
// Keep entries that have the age property
const cleanedUsers = users.filter(x => x.age)
为了得到用户的平均年龄,你可以将清理后的数组归约为一个数字,然后除以数组的长度。这可以在一行代码中完成。
const average = users.filter(x => x.age).reduce((acc,x) => acc + x.age, 0) / users.length
您可以按想要的键对其进行分组,并为每个组获取每个想要的 属性 的平均值。
function getAverages(array, groupKeys, averageKeys) {
var groups = {},
result = [];
array.forEach(o => {
var key = groupKeys.map(k => o[k]).join('|'),
group = groups[key];
if (!group) {
groups[key] = { count: 0, payload: {} };
group = groups[key];
averageKeys.forEach(k => group[k] = 0);
groupKeys.forEach(k => group.payload[k] = o[k]);
result.push(group.payload);
}
groups[key].count++;
averageKeys.forEach(k => group.payload[k] = (group[k] += o[k]) / group.count);
})
return result;
}
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 }];
console.log(getAverages(users, ['name', 'country'], ['age', 'weight']));
console.log(getAverages(users, ['name'], ['age', 'weight']));
.as-console-wrapper { max-height: 100% !important; top: 0; }