This is what I have (wealthByDistribution) and I require a solution like (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
}
]
Please if anyone can help me, I have been trying to solve it out for quite some time. I tried the following code, but it did not work as expected.
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];
}));
});
Companyproperty value? The last object is expected to beCompany: 0, "month": 7, "value": 110, is that really what you want? I'd have thoughtCompany: 110, "month": 7, "value": 0would be more inline with the rest of the logic you appear to be doing (also, where does the'value'property value come from in the expected output objects?)"value": 0come from, then?