将数组添加到数组项

时间:2019-05-30 11:49:57

标签: javascript arrays

我正在填充一个json文件,并且我有一个像这样的数组

[{ name: name, address: address, age: age }]

[{ name2: name2, address2: address2, age2: age2 }]

现在,我想合并这两个数组,并且期望得到这样的结果

[
    { name: name, address: address, age: age },
    { name2: name2, address2: address2, age2: age2 }
]

2 个答案:

答案 0 :(得分:2)

另一个选择...

var array1 = [{ name: name, address: address, age: age }]
var array2 = [{ name2: name2, address2: address2, age2: age2 }]

var merged = array1.concat(array2)

答案 1 :(得分:1)

    const a = [{ name: 'name1', address: 'address1', age: 22 }];
    
    const b = [{ name2: 'name2', address2: 'address2', age2: 23 }];
    
    const result = [...a, ...b];
    
    console.log(result);