gpt4 book ai didi

javascript - 从每个 javascript 数组 concat 中获取 1 个元素,然后循环到一个数组中

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

我有 4 个数组,我想将它们组合成 1 个。我可以这样做,但我想从每个数组中取出一个元素,将其推送到我的新数组,然后获取下一个 4,依此类推在。这是我得到的:

var a = [ "foo", "bar", "baz", "bam", "bun", "fun" ];
var b = [ 1, 2, 3, 4, 5, 6];
var c=["a","b","c","d","e","f"];
var d=[7,8,9,10,11,12]
var neat=[];
neat= a.concat(b, c,d);
//neat=["foo","bar","baz","bam","bun","fun",1,2,3,4,5,6,"a","b","c","d","e","f",7,8,9,10,11, 12]

我想要的结果是这样的:

//neat=["foo",1,"a",7,"bar",2,"b",8...]

我不确定循环是否有效,或者我是否需要使用另一个函数

最佳答案

假设每个源数组长度相同:

a.forEach((e, i) => {
neat.push(e, b[i], c[i], d[i]);
};

关于javascript - 从每个 javascript 数组 concat 中获取 1 个元素,然后循环到一个数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47604270/

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