javascript - 如何使用 javascript 将带有数组的对象加入到单个数组中?
问题描述
这就是我所拥有的(wealthByDistribution
),我需要像(expectedArray
)这样的解决方案。
const wealthByDistribution = {
CheckingAccount: [
{
"year": 2016,
"month": 4,
"value": 10
},
{
"year": 2016,
"month": 5,
"value": 0
},
{
"year": 2016,
"month": 6,
"value": 0
},
{
"year": 2016,
"month": 7,
"value": 0
}
],
Company: [
{
"year": 2016,
"month": 4,
"value": 0
},
{
"year": 2016,
"month": 5,
"value": 0
},
{
"year": 2016,
"month": 6,
"value": 0
},
{
"year": 2016,
"month": 7,
"value": 110
}
],
InvestmentAccount: [
{
"year": 2016,
"month": 4,
"value": 0
},
{
"year": 2016,
"month": 5,
"value": 0
},
{
"year": 2016,
"month": 6,
"value": 0
},
{
"year": 2016,
"month": 7,
"value": 220
}
],
InvestmentInsurance: [
{
"year": 2016,
"month": 4,
"value": 0
},
{
"year": 2016,
"month": 5,
"value": 0
},
{
"year": 2016,
"month": 6,
"value": 0
},
{
"year": 2016,
"month": 7,
"value": 330
}
],
Loan: [
{
"year": 2016,
"month": 4,
"value": 0
},
{
"year": 2016,
"month": 5,
"value": 0
},
{
"year": 2016,
"month": 6,
"value": 0
},
{
"year": 2016,
"month": 7,
"value": 0
}
],
PassionAssets: [
{
"year": 2016,
"month": 4,
"value": 0
},
{
"year": 2016,
"month": 5,
"value": 0
},
{
"year": 2016,
"month": 6,
"value": 0
},
{
"year": 2016,
"month": 7,
"value": 0
}
]
}
const returnExpectedArray = (wealthByDistribution) => {
const expectedArray = []
return expectedArray
}
const expectedArray = [
{
"year": 2016,
PassionAssets: 0,
Loan: 0,
InvestmentInsurance: 0,
InvestmentAccount: 0,
CheckingAccount: 10,
Company: 0,
"month": 4,
"value": 0
},
{
"year": 2016,
PassionAssets: 0,
Loan: 0,
InvestmentInsurance: 0,
InvestmentAccount: 0,
CheckingAccount: 0,
Company: 0,
"month": 5,
"value": 0
},
{
"year": 2016,
PassionAssets: 0,
Loan: 0,
InvestmentInsurance: 0,
InvestmentAccount: 0,
CheckingAccount: 0,
Company: 0,
"month": 6,
"value": 0
},
{
"year": 2016,
PassionAssets: 0,
Loan: 0,
InvestmentInsurance: 330,
InvestmentAccount: 220,
CheckingAccount: 0,
Company: 110,
"month": 7,
"value": 0
}
]
请如果有人可以帮助我,我已经尝试解决它很长一段时间了。我尝试了以下代码,但没有按预期工作。
const wealthByDistributionKeys = Object.keys(wealthByDistribution);
const [ key, ...rest ] = wealthByDistributionKeys;
const firstArray = wealthByDistribution[key] || [];
const expectedArray = firstArray.map((item, i) => {
item[key] = item.value;
return Object.assign({}, item, ...rest.map(r => {
wealthByDistribution[r][i][r] = wealthByDistribution[r][i].value;
return wealthByDistribution[r][i];
}));
});
解决方案
通过使用相应的键,您可以收集年/月的所有值并获得组合结果。
const
wealthByDistribution = { CheckingAccount: [{ year: 2016, month: 4, value: 10 }, { year: 2016, month: 5, value: 0 }, { year: 2016, month: 6, value: 0 }, { year: 2016, month: 7, value: 0 }], Company: [{ year: 2016, month: 4, value: 0 }, { year: 2016, month: 5, value: 0 }, { year: 2016, month: 6, value: 0 }, { year: 2016, month: 7, value: 110 }], InvestmentAccount: [{ year: 2016, month: 4, value: 0 }, { year: 2016, month: 5, value: 0 }, { year: 2016, month: 6, value: 0 }, { year: 2016, month: 7, value: 220 }], InvestmentInsurance: [{ year: 2016, month: 4, value: 0 }, { year: 2016, month: 5, value: 0 }, { year: 2016, month: 6, value: 0 }, { year: 2016, month: 7, value: 330 }], Loan: [{ year: 2016, month: 4, value: 0 }, { year: 2016, month: 5, value: 0 }, { year: 2016, month: 6, value: 0 }, { year: 2016, month: 7, value: 0 }], PassionAssets: [{ year: 2016, month: 4, value: 0 }, { year: 2016, month: 5, value: 0 }, { year: 2016, month: 6, value: 0 }, { year: 2016, month: 7, value: 0 }] },
result = Object.values(Object
.entries(wealthByDistribution)
.reduce((r, [k, a]) => {
a.forEach(({ year, month, value }) => {
const key = [year, month].join('|');
r[key] ??= { year, month };
r[key][k] = value;
});
return r;
}, {})
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
推荐阅读
- reactjs - ReactJS 上下文 > 从状态函数中引用最新状态值
- java - 使用 Java-Large 文件查询 JSON 文件
- reactjs - 如何通过reactjs在表格中正确显示json结果
- javascript - 使用 JS 修改表单标签文本,仅使用“for”属性作为选择器
- c# - Moq 出现 Null 引用错误并返回 IList
- c++ - Windows 10 上的 Cmake 错误“缺少:CURL_LIBRARY CURL_INCLUDE_DIR”
- docker - 致命:[xxxx]:失败!=> {"msg": "缺少 sudo 密码"}
- jenkins - 在 Jenkinsfile 中使用 withCredentials 时出错
- spring - spring boot:maven命令下载所有依赖
- python - PyQt5 QTabs 是唯一的解决方案