gpt4 book ai didi

javascript - 动态创建一个 javascript/jquery 多级数组

转载 作者:行者123 更新时间:2023-11-29 16:49:46 29 4
gpt4 key购买 nike

这让我发疯。

假设我有这个:

var tags = ["art","wedding"];
var containers = ["artcon","weddingcon"];
var limits = [5,15];

如何创建这种结构的多级数组?

var testarr = [
"art" = [
"container" = "artcon",
"limit" = 5,
],
"wedding" = [
"container" = "weddingcon",
"limit" = 15,
]
];

最好的问候。

最佳答案

您可以使用 Array#reduce并返回对象

var tags = ["art", "wedding"];
var containers = ["artcon", "weddingcon"];
var limits = [5, 15];

var result = tags.reduce((res, e, i) => {
res[e] = {container: containers[i], limit: limits[i]}
return res;
}, {})

console.log(result)

关于javascript - 动态创建一个 javascript/jquery 多级数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37212299/

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