如何用TypeScript中包含较新对象的数组替换对象的数组?

问题描述 投票:0回答:1

例如:我有一个像这样的对象数组:

let arrayOfObjects: [
    { "id": 0, "name": "Available" },
    { "id": 1, "name": "Ready" },
    { "id": 2, "name": "Started" }
];

现在我要用相同的数组替换或覆盖上面的数组,但是要包含不同的值(相同的键,不同的值):

let arrayOfObjects: [
    { "id": 0, "name": "Not Available" },
    { "id": 1, "name": "Not Ready" },
    { "id": 2, "name": "Not Started" }
];

如何在TypeScript中完成?

javascript arrays typescript data-structures higher-order-functions
1个回答
0
投票

尝试一下。

const oneArray = [
    { "id": 0, "name": "Available" },
    { "id": 1, "name": "Ready" },
    { "id": 2, "name": "Started" }
];

const twoArray = [
    { "id": 0, "name": "Not Available" },
    { "id": 1, "name": "Not Ready" },
    { "id": 2, "name": "Not Started" }
];

const newArray = Object.assign({}, oneArray, twoArray);
console.log(newArray);
© www.soinside.com 2019 - 2024. All rights reserved.