I Have two Array of Objects :
salesLabelData –
JavaScript
x
68
68
1
"salesData": [
2
{
3
"id": "weekly",
4
"chartData": {
5
"dataSets": [
6
{
7
"borderColor": "#2E87A8",
8
"backgroundColor": "#2E87A8",
9
"fill": "false",
10
"pointRadius": "3",
11
"pointHoverRadius": "4",
12
"borderWidth": "2"
13
},
14
{
15
"borderColor": "#951DAC",
16
"backgroundColor": "#951DAC",
17
"fill": "false",
18
"pointRadius": "3",
19
"pointHoverRadius": "4",
20
"borderWidth": "2"
21
},
22
{
23
"borderColor": "#FA9610",
24
"backgroundColor": "#FA9610",
25
"fill": "false",
26
"pointRadius": "3",
27
"pointHoverRadius": "4",
28
"borderWidth": "2"
29
}
30
]
31
}
32
},
33
{
34
"id": "monthly",
35
"chartData": {
36
"dataSets": [
37
{
38
"id": "target-qty",
39
"borderColor": "#2E87A8",
40
"backgroundColor": "#2E87A8",
41
"fill": "false",
42
"pointRadius": "3",
43
"pointHoverRadius": "4",
44
"borderWidth": "2"
45
},
46
{
47
"id": "net-sales",
48
"borderColor": "#951DAC",
49
"backgroundColor": "#951DAC",
50
"fill": "false",
51
"pointRadius": "3",
52
"pointHoverRadius": "4",
53
"borderWidth": "2"
54
},
55
{
56
"id": "gap",
57
"borderColor": "#FA9610",
58
"backgroundColor": "#FA9610",
59
"fill": "false",
60
"pointRadius": "3",
61
"pointHoverRadius": "4",
62
"borderWidth": "2"
63
}
64
]
65
}
66
}
67
]
68
salesAPIData :
JavaScript
1
80
80
1
"salesData": [
2
{
3
"id": "weekly",
4
"chartData": {
5
"labels": [
6
"Jan",
7
"Feb",
8
"Mar",
9
"Apr",
10
"May",
11
"Jun",
12
"July",
13
"Aug",
14
"Sep",
15
"Oct",
16
"Nov",
17
"Dec"
18
],
19
"datasets": [
20
{
21
"id": "target-qty",
22
"type": "bar",
23
"label": "Target Qty",
24
"data": [450, 480, 379, 325, 425, 287, 274, 499, 333, 401, 123, 444]
25
},
26
{
27
"id": "net-sales",
28
"type": "bar",
29
"label": "Net Sales Qty",
30
"data": [450, 480, 379, 325, 425, 287, 274, 499, 333, 401, 123, 444]
31
},
32
{
33
"id": "gap",
34
"type": "line",
35
"label": "Gap",
36
"data": [450, 480, 470, 420, 425, 436, 401, 411, 422, 433, 499, 444]
37
}
38
]
39
}
40
},
41
{
42
"id": "monthly",
43
"chartData": {
44
"labels": [
45
"Jan",
46
"Feb",
47
"Mar",
48
"Apr",
49
"May",
50
"Jun",
51
"July",
52
"Aug",
53
"Sep",
54
"Oct",
55
"Nov",
56
"Dec"
57
],
58
"datasets": [
59
{
60
"id": "target-qty",
61
"type": "bar",
62
"label": "Target Qty",
63
"data": [450, 480, 379, 325, 425, 287, 274, 499, 333, 401, 123, 444]
64
},
65
{
66
"id": "net-sales",
67
"type": "bar",
68
"label": "Net Sales Qty",
69
"data": [450, 480, 379, 325, 425, 287, 274, 499, 333, 401, 123, 444]
70
},
71
{
72
"id": "gap",
73
"type": "line",
74
"label": "Gap",
75
"data": [450, 480, 470, 420, 425, 436, 401, 411, 422, 433, 499, 444]
76
}
77
]
78
}
79
}
80
]
I need to merge these, to eventually get this array (Basically the Datasets have to be merged into one) :
Expected Result –
JavaScript
1
163
163
1
"salesData": [
2
{
3
"id": "weekly",
4
"chartData": {
5
"labels": [
6
"Jan",
7
"Feb",
8
"Mar",
9
"Apr",
10
"May",
11
"Jun",
12
"July",
13
"Aug",
14
"Sep",
15
"Oct",
16
"Nov",
17
"Dec"
18
],
19
"datasets": [
20
{
21
"id": "target-qty",
22
"type": "bar",
23
"label": "Target Qty",
24
"data": [
25
450,
26
480,
27
379,
28
325,
29
425,
30
287,
31
274,
32
499,
33
333,
34
401,
35
123,
36
444
37
],
38
"borderColor": "#2E87A8",
39
"backgroundColor": "#2E87A8",
40
"fill": "false",
41
"pointRadius": "3",
42
"pointHoverRadius": "4",
43
"borderWidth": "2"
44
},
45
{
46
"id": "net-sales",
47
"type": "bar",
48
"label": "Net Sales Qty",
49
"data": [
50
450,
51
480,
52
379,
53
325,
54
425,
55
287,
56
274,
57
499,
58
333,
59
401,
60
123,
61
444
62
],
63
"borderColor": "#951DAC",
64
"backgroundColor": "#951DAC",
65
"fill": "false",
66
"pointRadius": "3",
67
"pointHoverRadius": "4",
68
"borderWidth": "2"
69
},
70
{
71
"id": "gap",
72
"type": "line",
73
"label": "Gap",
74
"data": [
75
450,
76
480,
77
470,
78
420,
79
425,
80
436,
81
401,
82
411,
83
422,
84
433,
85
499,
86
444
87
],
88
"borderColor": "#FA9610",
89
"backgroundColor": "#FA9610",
90
"fill": "false",
91
"pointRadius": "3",
92
"pointHoverRadius": "4",
93
"borderWidth": "2"
94
}
95
]
96
}
97
},
98
{
99
"id": "monthly",
100
"labelName": "TARGET",
101
"chartData": {
102
"labels": [
103
"Jan",
104
"Feb",
105
"Mar",
106
"Apr",
107
"May",
108
"Jun",
109
"July",
110
"Aug",
111
"Sep",
112
"Oct",
113
"Nov",
114
"Dec"
115
],
116
"datasets": [
117
{
118
"id": "target-qty",
119
"type": "bar",
120
"label": "Target Qty",
121
"data": [
122
950, 980, 379, 325, 925, 287, 279, 999, 333, 901, 123, 999
123
],
124
"borderColor": "#2E87A8",
125
"backgroundColor": "#2E87A8",
126
"fill": "false",
127
"pointRadius": "3",
128
"pointHoverRadius": "4",
129
"borderWidth": "2"
130
},
131
{
132
"id": "net-sales",
133
"type": "bar",
134
"label": "Net Sales Qty",
135
"data": [
136
950, 980, 379, 325, 925, 287, 279, 999, 333, 901, 123, 999
137
],
138
"borderColor": "#951DAC",
139
"backgroundColor": "#951DAC",
140
"fill": "false",
141
"pointRadius": "3",
142
"pointHoverRadius": "4",
143
"borderWidth": "2"
144
},
145
{
146
"id": "gap",
147
"type": "line",
148
"label": "Gap",
149
"data": [
150
950, 980, 379, 325, 925, 287, 279, 999, 333, 901, 123, 999
151
],
152
"borderColor": "#FA9610",
153
"backgroundColor": "#FA9610",
154
"fill": "false",
155
"pointRadius": "3",
156
"pointHoverRadius": "4",
157
"borderWidth": "2"
158
}
159
]
160
}
161
}
162
]
163
I have tried the following and other various permutations/combinations, checked out many answers on this site but none worked:
JavaScript
1
14
14
1
if (salesLabelData?.salesData && salesAPIData?.salesData) {
2
const array1 = salesLabelData.salesData;
3
const array2 = salesAPIData.salesData;
4
array1?.map((data, index) => {
5
if (data.id === array2[index].id) {
6
const labelData = {
7
data,
8
array2[index],
9
};
10
salesBarChartData.push(labelData);
11
}
12
return salesBarChartData;
13
});
14
}
I am missing something, not sure what and hence am not able to get the desired result, Any Help is sincerely appreciated.
Advertisement
Answer
You’ll need to have a kind of deep merge. It seems that we can assume that:
- the data types of data that occurs in the same place in both data structures, are guaranteed to be the same;
- the arrays that occur in the same place in both data structures, are guaranteed to have the same size;
- the primitive values that occur in the same place in both data structures (strings, numbers, …) are guaranteed to be the same.
It’s too bad that you want to merge properties that have different names (dataSets
, datasets
), so some code is needed to deal with that. But it would be better to correct this at the source.
Here is some suggested code:
JavaScript
1
21
21
1
function deepMerge(a, b) {
2
if (Object(a) !== a) return b;
3
if (Object(b) !== b) return a;
4
if (Array.isArray(a)) return a.map((obj, i) => deepMerge(obj, b[i]));
5
if (a.hasOwnProperty("dataSets")) { // "fix"
6
let dataSets;
7
({ dataSets, a } = { a, datasets: dataSets });
8
}
9
return Object.fromEntries(Array.from(
10
new Set(Object.keys(a).concat(Object.keys(b))),
11
key => [key, deepMerge(a[key], b[key])]
12
));
13
}
14
15
var salesLabelData = {"salesData": [{"id": "weekly","chartData": {"dataSets": [{"borderColor": "#2E87A8","backgroundColor": "#2E87A8","fill": "false","pointRadius": "3","pointHoverRadius": "4","borderWidth": "2"},{"borderColor": "#951DAC","backgroundColor": "#951DAC","fill": "false","pointRadius": "3","pointHoverRadius": "4","borderWidth": "2"},{"borderColor": "#FA9610","backgroundColor": "#FA9610","fill": "false","pointRadius": "3","pointHoverRadius": "4","borderWidth": "2"}]}},{"id": "monthly","chartData": {"dataSets": [{"id": "target-qty","borderColor": "#2E87A8","backgroundColor": "#2E87A8","fill": "false","pointRadius": "3","pointHoverRadius": "4","borderWidth": "2"},{"id": "net-sales","borderColor": "#951DAC","backgroundColor": "#951DAC","fill": "false","pointRadius": "3","pointHoverRadius": "4","borderWidth": "2"},{"id": "gap","borderColor": "#FA9610","backgroundColor": "#FA9610","fill": "false","pointRadius": "3","pointHoverRadius": "4","borderWidth": "2"}]}}]};
16
17
var salesAPIData = {"salesData": [{"id": "weekly","chartData": {"labels": ["Jan","Feb","Mar","Apr","May","Jun","July","Aug","Sep","Oct","Nov","Dec"],"datasets": [{"id": "target-qty","type": "bar","label": "Target Qty","data": [450, 480, 379, 325, 425, 287, 274, 499, 333, 401, 123, 444]},{"id": "net-sales","type": "bar","label": "Net Sales Qty","data": [450, 480, 379, 325, 425, 287, 274, 499, 333, 401, 123, 444]},{"id": "gap","type": "line","label": "Gap","data": [450, 480, 470, 420, 425, 436, 401, 411, 422, 433, 499, 444]}]}},{"id": "monthly","chartData": {"labels": ["Jan","Feb","Mar","Apr","May","Jun","July","Aug","Sep","Oct","Nov","Dec"],"datasets": [{"id": "target-qty","type": "bar","label": "Target Qty","data": [450, 480, 379, 325, 425, 287, 274, 499, 333, 401, 123, 444]},{"id": "net-sales","type": "bar","label": "Net Sales Qty","data": [450, 480, 379, 325, 425, 287, 274, 499, 333, 401, 123, 444]},{"id": "gap","type": "line","label": "Gap","data": [450, 480, 470, 420, 425, 436, 401, 411, 422, 433, 499, 444]}]}}]}
18
19
let result = deepMerge(salesLabelData, salesAPIData);
20
21
console.log(result);