I’m having a bit of an issue figuring this out.
I have an array object per customer, per year of the month:
[
{
color: "#009192",
data: [0, 0, 57.62, 0, 0, 0, 0, 0, 0, 0, 0, 0],
label: "Client 1"
},
{
color: "#009192",
data: [0, 0, 234.65, 0, 0, 0, 0, 0, 0, 0, 0, 0],
label: "Client 2"
},
{
color: "#009192"
data: [0, 0, 490.43, 0, 0, 0, 0, 0, 0, 0, 0, 0]
label: "Client 3"
},
{
color: "#009192"
data: [0, 0, 0, 94.00, 0, 0, 0, 0, 0, 0, 0, 0]
label: "Client 1"
}
]
How do I add the two objects with the same label (Client 1) and create a new array that would look like this?
[
{
color: "#009192",
data: [0, 0, 57.62, 94.00, 0, 0, 0, 0, 0, 0, 0, 0],
label: "Client 1"
},
{
color: "#009192",
data: [0, 0, 234.65, 0, 0, 0, 0, 0, 0, 0, 0, 0],
label: "Client 2"
},
{
color: "#009192"
data: [0, 0, 490.43, 0, 0, 0, 0, 0, 0, 0, 0, 0]
label: "Client 3"
},
]
Thank you!
2
Answers
You can use
Array#reduce
with an object to store the values for each label and sum thedata
array for objects with the same same label.Hope this can help you
Regards