admin管理员组文章数量:1335624
var dictA = { male: 10, female: 20, unassigned: 30 };
var dictB = { male: 11, female: 21, unassigned: 31 };
var dictC = { male: 12, female: 22, unassigned: 32 };
Is there an easier way than multiple loops to produce a result like follows:
{
male: [10, 11, 12],
female: [20, 21, 22],
unassigned: [30, 31, 32]
}
I am not sure if 'bine' is the right word here.
var dictA = { male: 10, female: 20, unassigned: 30 };
var dictB = { male: 11, female: 21, unassigned: 31 };
var dictC = { male: 12, female: 22, unassigned: 32 };
Is there an easier way than multiple loops to produce a result like follows:
{
male: [10, 11, 12],
female: [20, 21, 22],
unassigned: [30, 31, 32]
}
I am not sure if 'bine' is the right word here.
Share Improve this question asked Dec 4, 2019 at 2:53 Steven FloydSteven Floyd 3531 gold badge3 silver badges11 bronze badges 3- FYI, in JS we usually call these Objects, not dictionaries or lists :) – user7886229 Commented Dec 4, 2019 at 3:12
- See also: stackoverflow./questions/59122448/… – JLRishe Commented Dec 4, 2019 at 4:26
- Does this answer your question? How to group an array of objects by key – alexyorke Commented Dec 4, 2019 at 4:38
5 Answers
Reset to default 6This still requires a couple loops, but perhaps a bination of reduce
and for...in
will get this done eloquently:
var dictA = { male: 10, female: 20, unassigned: 30 };
var dictB = { male: 11, female: 21, unassigned: 31 };
var dictC = { male: 12, female: 22, unassigned: 32 };
const res = [dictA, dictB, dictC].reduce((acc, el) => {
for (let key in el) {
acc[key] = [...acc[key] || [], el[key]];
};
return acc;
}, {})
console.log(res);
This works assuming dictA
has all of the necessary properties. It's not as functional like @Nicks.
const dictA = { male: 10, female: 20, unassigned: 30 };
const dictB = { male: 11, female: 21, unassigned: 31 };
const dictC = { male: 12, female: 22, unassigned: 32 };
const obj = {};
Object.keys(dictA).forEach(key => {
obj[key] = [dictA,dictB,dictC].map(dict => dict[key]);
});
console.log(obj);
var dictA = { male: 10, female: 20, unassigned: 30 };
var dictB = { male: 11, female: 21, unassigned: 31 };
var dictC = { male: 12, female: 22, unassigned: 32 };
var input = [dictA, dictB, dictC];
var output = [];
input.forEach(function(item) {
var existing = output.filter(function(v, i) {
return v.name == item.name;
});
if (existing.length) {
var existingIndex = output.indexOf(existing[0]);
// output[existingIndex].value = output[existingIndex].value.concat(item.value);
} else {
if (typeof item.value == 'string')
item.value = [item.value];
output.push(item);
}
});
alert(output[0].male);
You could do it with .reduce()
and .forEach()
too
let result = [dictA, dictB, dictC].reduce((rv, dict) => (
Object.keys(dict).forEach(key => (
rv[key] != null ? rv[key].push(dict[key]) : rv[key] = [dict[key]]
)),
rv
), {});
const addToBucket = (bucket, [k, v], prev = bucket[k] || []) => ({
...bucket, [k]: [...prev, v]
})
const bucket = list => list.flatMap(Object.entries).reduce(
(bucket, entry) => addToBucket(bucket, entry), {}
)
// ---------------------------------------------------------- //
console.log(bucket([
{ male: 10, female: 20, unassigned: 30 },
{ male: 11, female: 21, unassigned: 31 },
{ male: 12, female: 22, unassigned: 32 },
]))
本文标签: listHow to combine multiple dictionaries in JavascriptStack Overflow
版权声明:本文标题:list - How to combine multiple dictionaries in Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742310976a2450864.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论