I have two JSON objects with the same structure/value and I want to concat them together using Javascript.
My first array looks like this:
[{
"name": "Spain",
"year": [
"2010"
]
},
{
"name": "Brazil",
"year": [
"1994",
"1970",
"1962",
"2002",
"1958"
]
},
{
"name": "Germany",
"year": [
"2014",
"1990",
"1974",
"1954"
]
},
{
"name": "Italy",
"year": [
"2006",
"1982",
"1938",
"1934"
]
},
{
"name": "France",
"year": [
"2018",
"1998"
]
},
{
"name": "Argentina",
"year": [
"1986",
"1978"
]
},
{
"name": "Uruguay",
"year": [
"1930",
"1950"
]
},
{
"name": "England",
"year": [
"1966"
]
}
]
My second array:
[{
"name": "Spain",
"anzahl": 1
},
{
"name": "Brazil",
"anzahl": 5
},
{
"name": "Germany",
"anzahl": 4
},
{
"name": "Italy",
"anzahl": 4
},
{
"name": "France",
"anzahl": 2
},
{
"name": "Argentina",
"anzahl": 2
},
{
"name": "Uruguay",
"anzahl": 2
},
{
"name": "England",
"anzahl": 1
}
]
I am looking for a solution to merge all the same keys and add the values of the merged keys together to get something looking like this:
[{
"name": "Spain",
"year": [
"2010"
]
"anzahl": 1
},
{
"name": "Brazil",
"year": [
"1994",
"1970",
"1962",
"2002",
"1958"
]
"anzahl": 5
},
.....
]
Any help would be awesome, thanks :)