简体   繁体   中英

Merge two arrays together in JS

I have two arrays and I want to add both. Is it possible to get this:

var frist = {
    "2162018": [
        {
            st_id: "18ds1",
            status: "A",
            today_milli: 1529570651.113
        },
        {
            st_id: "18ds1",
            status: "A",
            today_milli: 1529570827.709
        }
    ]
};
var second = {
    "2362018": [
        {
            st_id: "18ds1",
            status: "A",
            today_milli: 1529574243.613
        }
    ]
};
//and i want my final data array like this 
var final_data = {
    "2162018": [
        {
            st_id: "18ds1",
            status: "A",
            today_milli: 1529570651.113
        },
        {
            st_id: "18ds1",
            status: "A",
            today_milli: 1529570827.709
        }
    ],
    "2362018": [
        {
            st_id: "18ds1",
            status: "A",
            today_milli: 1529574243.613
        }
    ]
};

 var frist = { '2162018': [ { st_id: '18ds1', status: 'A', today_milli: 1529570651.113 }, { st_id: '18ds1', status: 'A', today_milli: 1529570827.709 } ] } var second = { '2362018': [ { st_id: '18ds1', status: 'A', today_milli: 1529574243.613 } ] } let final_data = {...frist,...second} console.log(final_data) 

Yes you can simply using spread syntax to combine and construct new object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM