![](/img/trans.png)
[英]How can I perform an inner join with two object arrays in JavaScript?
[英]How can I join an object with arrays into a single array using 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; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.