This is what I have (wealthByDistribution
) and I require a solution like (expectedArray
).
JavaScript
x
187
187
1
const wealthByDistribution = {
2
CheckingAccount: [
3
{
4
"year": 2016,
5
"month": 4,
6
"value": 10
7
},
8
{
9
"year": 2016,
10
"month": 5,
11
"value": 0
12
},
13
{
14
"year": 2016,
15
"month": 6,
16
"value": 0
17
},
18
{
19
"year": 2016,
20
"month": 7,
21
"value": 0
22
}
23
],
24
Company: [
25
{
26
"year": 2016,
27
"month": 4,
28
"value": 0
29
},
30
{
31
"year": 2016,
32
"month": 5,
33
"value": 0
34
},
35
{
36
"year": 2016,
37
"month": 6,
38
"value": 0
39
},
40
{
41
"year": 2016,
42
"month": 7,
43
"value": 110
44
}
45
],
46
InvestmentAccount: [
47
{
48
"year": 2016,
49
"month": 4,
50
"value": 0
51
},
52
{
53
"year": 2016,
54
"month": 5,
55
"value": 0
56
},
57
{
58
"year": 2016,
59
"month": 6,
60
"value": 0
61
},
62
{
63
"year": 2016,
64
"month": 7,
65
"value": 220
66
}
67
],
68
InvestmentInsurance: [
69
{
70
"year": 2016,
71
"month": 4,
72
"value": 0
73
},
74
{
75
"year": 2016,
76
"month": 5,
77
"value": 0
78
},
79
{
80
"year": 2016,
81
"month": 6,
82
"value": 0
83
},
84
{
85
"year": 2016,
86
"month": 7,
87
"value": 330
88
}
89
],
90
Loan: [
91
{
92
"year": 2016,
93
"month": 4,
94
"value": 0
95
},
96
{
97
"year": 2016,
98
"month": 5,
99
"value": 0
100
},
101
{
102
"year": 2016,
103
"month": 6,
104
"value": 0
105
},
106
{
107
"year": 2016,
108
"month": 7,
109
"value": 0
110
}
111
],
112
PassionAssets: [
113
{
114
"year": 2016,
115
"month": 4,
116
"value": 0
117
},
118
{
119
"year": 2016,
120
"month": 5,
121
"value": 0
122
},
123
{
124
"year": 2016,
125
"month": 6,
126
"value": 0
127
},
128
{
129
"year": 2016,
130
"month": 7,
131
"value": 0
132
}
133
]
134
}
135
136
const returnExpectedArray = (wealthByDistribution) => {
137
const expectedArray = []
138
return expectedArray
139
}
140
141
const expectedArray = [
142
{
143
"year": 2016,
144
PassionAssets: 0,
145
Loan: 0,
146
InvestmentInsurance: 0,
147
InvestmentAccount: 0,
148
CheckingAccount: 10,
149
Company: 0,
150
"month": 4,
151
"value": 0
152
},
153
{
154
"year": 2016,
155
PassionAssets: 0,
156
Loan: 0,
157
InvestmentInsurance: 0,
158
InvestmentAccount: 0,
159
CheckingAccount: 0,
160
Company: 0,
161
"month": 5,
162
"value": 0
163
},
164
{
165
"year": 2016,
166
PassionAssets: 0,
167
Loan: 0,
168
InvestmentInsurance: 0,
169
InvestmentAccount: 0,
170
CheckingAccount: 0,
171
Company: 0,
172
"month": 6,
173
"value": 0
174
},
175
{
176
"year": 2016,
177
PassionAssets: 0,
178
Loan: 0,
179
InvestmentInsurance: 330,
180
InvestmentAccount: 220,
181
CheckingAccount: 0,
182
Company: 110,
183
"month": 7,
184
"value": 0
185
}
186
]
187
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.
JavaScript
1
13
13
1
const wealthByDistributionKeys = Object.keys(wealthByDistribution);
2
const [ key, rest ] = wealthByDistributionKeys;
3
const firstArray = wealthByDistribution[key] || [];
4
5
const expectedArray = firstArray.map((item, i) => {
6
item[key] = item.value;
7
return Object.assign({}, item, rest.map(r => {
8
wealthByDistribution[r][i][r] = wealthByDistribution[r][i].value;
9
return wealthByDistribution[r][i];
10
}));
11
});
12
13
Advertisement
Answer
By using corresponding keys, you could collect all value with year/month and get a combined result.
JavaScript
1
15
15
1
const
2
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 }] },
3
result = Object.values(Object
4
.entries(wealthByDistribution)
5
.reduce((r, [k, a]) => {
6
a.forEach(({ year, month, value }) => {
7
const key = [year, month].join('|');
8
r[key] ??= { year, month };
9
r[key][k] = value;
10
});
11
return r;
12
}, {})
13
);
14
15
console.log(result);
JavaScript
1
1
1
.as-console-wrapper { max-height: 100% !important; top: 0; }