如何使用相同的键组合对象而不覆盖值?
How to Combine an object with the same key and NOT override the values?
我正在尝试通过相同的键组合一组对象,而不是覆盖值。是否可以将这些值组合到一个数组中以指向相同的键引用。
考虑这个日历对象:
const calendar = [
{date: "1999", month: "03"},
{date: "1999", month: "01"},
{date: "1998", month: "04"}
]
// My attempted with lodash:
let results = _(calendar )
.groupBy('date')
.value()
// my output (also returns an object)
results = {
1998: {date: "1998", month: "04"}
1999: {date: "1999", month: "03"}, {date: "1999", month: "01"}
}
// Acceptable Output #1:
results = [
{date: "1999", month: ["03", "01"]},
{date: "1998", month: ["04"]}
]
// Acceptable Output #2:
results = {
1998: [{month: "04"}]
1999: [{month: "03"}, {month: "01"}]
}
// Acceptable Output #3:
results = [
{"1999": ["03", "01"]},
{"1998": ["04"]}
]
或者,如果有其他替代方案也能正常工作。最终目标是不覆盖我在整个 Whosebug 看到的值...
您可以使用 reduce
.
而不是使用 lodash
库
Output 3
const calendar = [
{date: "1999", month: "03"},
{date: "1999", month: "01"},
{date: "1998", month: "04"}
];
const output = calendar
.reduce((a, {date, month}) => {
if(!a[date]) {
a[date] = [];
}
a[date].push(month);
return a;
}, {});
console.log(output);
Output 2
const calendar = [
{date: "1999", month: "03"},
{date: "1999", month: "01"},
{date: "1998", month: "04"}
];
const output = calendar
.reduce((a, {date, month}) => {
if(!a[date]) {
a[date] = [];
}
a[date].push({month});
return a;
}, {});
console.log(output);
Output 1
const calendar = [
{date: "1999", month: "03"},
{date: "1999", month: "01"},
{date: "1998", month: "04"}
];
const output = calendar
.reduce((a, {date, month}) => {
if(!a[date]) {
a[date] = {date, month: []};
}
a[date].month.push(month);
return a;
}, {});
console.log(Object.values(output));
我正在尝试通过相同的键组合一组对象,而不是覆盖值。是否可以将这些值组合到一个数组中以指向相同的键引用。
考虑这个日历对象:
const calendar = [
{date: "1999", month: "03"},
{date: "1999", month: "01"},
{date: "1998", month: "04"}
]
// My attempted with lodash:
let results = _(calendar )
.groupBy('date')
.value()
// my output (also returns an object)
results = {
1998: {date: "1998", month: "04"}
1999: {date: "1999", month: "03"}, {date: "1999", month: "01"}
}
// Acceptable Output #1:
results = [
{date: "1999", month: ["03", "01"]},
{date: "1998", month: ["04"]}
]
// Acceptable Output #2:
results = {
1998: [{month: "04"}]
1999: [{month: "03"}, {month: "01"}]
}
// Acceptable Output #3:
results = [
{"1999": ["03", "01"]},
{"1998": ["04"]}
]
或者,如果有其他替代方案也能正常工作。最终目标是不覆盖我在整个 Whosebug 看到的值...
您可以使用 reduce
.
lodash
库
Output 3
const calendar = [
{date: "1999", month: "03"},
{date: "1999", month: "01"},
{date: "1998", month: "04"}
];
const output = calendar
.reduce((a, {date, month}) => {
if(!a[date]) {
a[date] = [];
}
a[date].push(month);
return a;
}, {});
console.log(output);
Output 2
const calendar = [
{date: "1999", month: "03"},
{date: "1999", month: "01"},
{date: "1998", month: "04"}
];
const output = calendar
.reduce((a, {date, month}) => {
if(!a[date]) {
a[date] = [];
}
a[date].push({month});
return a;
}, {});
console.log(output);
Output 1
const calendar = [
{date: "1999", month: "03"},
{date: "1999", month: "01"},
{date: "1998", month: "04"}
];
const output = calendar
.reduce((a, {date, month}) => {
if(!a[date]) {
a[date] = {date, month: []};
}
a[date].month.push(month);
return a;
}, {});
console.log(Object.values(output));