gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:59:25 28 4
gpt4 key购买 nike

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

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 中完成?

最佳答案

试试这个 JS

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

let newArray = Object.assign([], oneArray, twoArray);
console.log(newArray);

在TS中

interface Data {
oneArray: array;
twoArray: array;
}

function merge(data: Data) {
return Object.assign([], data.oneArray, data.twoArray);
}

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

console.log(merge(user));

关于javascript - 如何用包含 TypeScript 中较新对象的数组替换对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58521866/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com