在JS中合并两个数组

时间:2018-06-21 09:56:50

标签: javascript

我有两个数组,我想两个都加。是否有可能得到这个:

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
        }
    ]
};

1 个答案:

答案 0 :(得分:1)

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)

是的,您可以简单地使用传播语法来组合和构造新对象