2

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 :)

2 Answers 2

2

Map through the first array and use Object.assign to assign the properties from the item with the same name property in the second array (with Array.find) to the current item:

const arr1=[{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"]}],arr2=[{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}]

const result = arr1.map(e => Object.assign(e, arr2.find(f => f.name == e.name)))
console.log(result)

Sign up to request clarification or add additional context in comments.

Comments

1

A naive solution to work with any number of dynamic keys:

Solution Link

const json1 = [{
    name: "test1",
    city: [
    ]
}, {
    name: "test2",
    city: [
    ]
}];
const json2 = [{
    age: "19",
    address:"cb usa"
}, {
    age: "20"
}];
let t = Object.assign({}, json1);
for(let i=0;i< json1.length;i++) {
     let l = Object.keys(json2[i]);
     for(let j=0;j< l.length;j++) {
           t[i][l[j]] =json2[i][l[j]] ;
    }   
}
console.log(t);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.