如何使用AngularJs合并两个JavaScript数组

时间:2018-04-06 12:13:59

标签: javascript angularjs

我想合并两个JavaScript对象。

device: [{devHardwareId:"40A8F065E465",deviceId:67,deviceIp:"192.168.2.3",deviceName:"DESKTOP-IV0H659"}]

peripherals:[{
    "peripheralId": 3,
    "perHardwareId": "AVtech-Cam",
    "peripheralName": "BackGate",
    "peripheralType": "Video",
    "isActive": true,
},
{
    "peripheralId": 1,
    "perHardwareId": "Logitech-Cam",
    "peripheralName": "FrontCam",
    "peripheralType": "Video",
    "isActive": true,
}]

在上面的例子中,该设备可以包含几个外围设备。我想使用AngularJs合并它。然后它应该如下所示。

 device :{devHardwareId:"40A8F065E465",
deviceId:67,
deviceIp:"192.168.2.3",
deviceName:"DESKTOP-IV0H659",
peripherals:{
    "peripheralId": 3,
    "perHardwareId": "AVtech-Cam",
    "peripheralName": "BackGate",
    "peripheralType": "Video",
    "isActive": true,
},
{
    "peripheralId": 1,
    "perHardwareId": "Logitech-Cam",
    "peripheralName": "FrontCam",
    "peripheralType": "Video",
    "isActive": true,
}
}

如何使用AngularJs完成该任务。

1 个答案:

答案 0 :(得分:2)

AngularJS与此无关。您可以使用简单的JavaScript:

let device = [{"devHardwareId":"40A8F065E465","deviceId":67,"deviceIp":"192.168.2.3","deviceName":"DESKTOP-IV0H659"}];
const peripherals = [{"peripheralId":3,"perHardwareId":"AVtech-Cam","peripheralName":"BackGate","peripheralType":"Video","isActive":true},{"peripheralId":1,"perHardwareId":"Logitech-Cam","peripheralName":"FrontCam","peripheralType":"Video","isActive":true}];

device[0].peripherals = peripherals;
console.log(device[0]);