gpt4 book ai didi

javascript - 我如何从这个起始数组创建这个数组?

转载 作者:行者123 更新时间:2023-11-28 17:13:35 25 4
gpt4 key购买 nike

您好,我有这个起始数组:

names = [ '1A', '1B', '1C', '1D', '1E'];

我稍后需要在我的申请中查看轮到谁了。我需要能够将一些值存储在不同类型的数组中,如下所示:

scores = [
{
id: "1A",
endScores: {}
},
{
id: "1B",
endScores: {}
},
{
id: "1C",
endScores: {}
},
{
id: "1D",
endScores: {}
},
{
id: "1E",
endScores: {}
}
];

如何从我的第一个数组转到第二个数组?第一个数组的大小也可以改变,其中可以有更多或更少的名称......希望有一种可能的方法。 PS:我使用的是 Angular 2。

最佳答案

这是您将 names 数组映射到所需集合的方法。

const names = [ '1A', '1B', '1C', '1D', '1E'];

const transformToScores = names => names.map(id => ({id, endScores: {}}))

const scores = transformToScores(names)

console.log(scores)

关于javascript - 我如何从这个起始数组创建这个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53959618/

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