gpt4 book ai didi

javascript - 两个数组结果在 Javascript 中创建新数组

转载 作者:行者123 更新时间:2023-12-02 22:46:20 27 4
gpt4 key购买 nike

这是我的两个数据数组:

var array1 = ["test","test1","test2"];
var array2 = ["test3","test4","test5"];

我需要连接数组以获得以下结果:

array3["test","test3","test1","test4","test2","test5"]

如何存档?

最佳答案

考虑到,两个数组可以有不同数量的元素

var array1 = ["test","test1","test2"];
var array2 = ["test3","test4","test5"];
var maxLength = Math.max(array1.length, array2.length)
var array3 = [];
for (var index=0; index < maxLength; index++) {
if (index < array1.length)
array3.push(array1[index]);
if (index < array2.length)
array3.push(array2[index]);
}
console.log(array3);

关于javascript - 两个数组结果在 Javascript 中创建新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58388464/

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