gpt4 book ai didi

javascript - 对象数组的 lodash 总和

转载 作者:行者123 更新时间:2023-11-28 17:42:32 24 4
gpt4 key购买 nike

我刚刚开始学习 lodash,并尝试将其合并到我的代码中。正如您所看到的,我正在添加员工下每个元素的权重。问题是我遇到的问题是我不确定如何将第三个元素“性别”放入我的最终数据数组中。我用过这个Reduce array of objects on key and sum value into array构建我的 lodash 代码。您可以在下面看到我如何尝试获取输出。

var userAns = [{
id: 1,
text: "answer1",
employee: [{
name: "John",
weight: 3,
gender: "male"
},
{
name: "Sally",
weight: 4,
gender: "female"
}
]
},
{
id: 2,
text: "answer2",
employee: [{
name: "John",
weight: 6,
gender: "male"
},
{
name: "Sally",
weight: 3,
gender: "female"
}
]
}
];

var scores = [];
for (var i = 0; i < userAns.length; i++) {
for (var j = 0; j < userAns[i].employee.length; j++) {
scores.push(userAns[i].employee[j]);
}
}

var result = _(scores)
.map('name')
.uniq()
.map(function(key) {

return {
name: key,
score: _(scores).filter({
name: key
}).sumBy('weight')
};
})
.value();

console.log(result);
<script src="https://cdn.jsdelivr.net/lodash/4.14.1/lodash.min.js"></script>

这是我尝试获取数组的方法

[
{
"name": "John",
"score": 9,
"gender": "male"
},
{
"name": "Sally",
"score": 7,
"gender":"female"
}
]

最佳答案

使用 lodash:

您可以使用 _.flatMap() 的组合, _.groupBy() , _.map()_.mergeWith() :

var userAns = [{"id":1,"text":"answer1","employee":[{"name":"John","weight":3,"gender":"male"},{"name":"Sally","weight":4,"gender":"female"}]},{"id":2,"text":"answer2","employee":[{"name":"John","weight":6,"gender":"male"},{"name":"Sally","weight":3,"gender":"female"}]}];

var result = _(userAns)
.flatMap('employee') // create an array of all emplyoees
.groupBy('name') // create groups of employees with the same name
.map(function(g) {
// merge each group to a single object, and combine weight values
return _.mergeWith.apply(_, [{}].concat(g, function(obj, src, key) {
return key === 'weight' ? (obj || 0) + src : undefined;
}));
})
.value();

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

使用 ES6:

使用Array#map获取 employee 数组的数组,并按 spreading 展平进入Array#concatReduce数组变成 Map ,然后通过扩展 Map#values 转换回数组迭代器:

const userAns = [{"id":1,"text":"answer1","employee":[{"name":"John","weight":3,"gender":"male"},{"name":"Sally","weight":4,"gender":"female"}]},{"id":2,"text":"answer2","employee":[{"name":"John","weight":6,"gender":"male"},{"name":"Sally","weight":3,"gender":"female"}]}];

const result = [...
[].concat(...userAns.map(({ employee }) => employee)) // combine all employees to a single array
.reduce((m, o) => { // reduce similar employees to a single object in a Map, and combine weight
// if the name object doesn't exists create a new one by cloning the object, and assigning weight: 0
m.has(o.name) || m.set(o.name, Object.assign({}, o, { weight: 0 }));

m.get(o.name).weight += o.weight; // add the current weight to the name object

return m;
}, new Map())
.values()]; // get the Map values, and spread to convert to array

console.log(result);

关于javascript - 对象数组的 lodash 总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47604996/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com