gpt4 book ai didi

javascript - 合并两个数组,使第一个索引都在新数组中,依此类推

转载 作者:行者123 更新时间:2023-11-30 07:20:49 24 4
gpt4 key购买 nike

我有两个这样的数组

第一个数组是

array1[
0:"7@a.com"
1:"6@live.com"
2:"5@live.com"
3:"55@a.com"
4:"4@live.com"
5:"3@live.com"
6:"62@a.com"
7:"61@a.com"
8:"61@a.com"
9:"59@a.com"
10:"58@a.com"

然后像这样以同样的方式排列两个

0"5223b14d-1682-4777-8ada-c5b0c972756f"
1:"290c79e8-62da-46f1-bff5-157031a079fa"
2:"eb4ff5f9-82c7-4095-8116-1c34f1755d06"
3:"987c51f4-23f1-4783-8061-a47dfff16fe3"
4:"afcf5ef1-a87d-465f-bb1b-f8db81ecc178"
5:"2d299d60-9481-4c3f-9b9b-e6659ee74d07"
6:"1f40b5ef-990f-4edd-925c-b511dd64899f"
7:"88de17fd-286f-4960-9e4f-a2b01425da82"
8:"1bb181cb-ab7c-47af-95a6-99357459e6a3"
9:"9554f7c2-a20d-4ff7-add6-8840b2d06a89"
10:"15e51955-9eb9-4c4b-990f-97e29820e04d"

现在我想制作 10 个新数组,其中包含两者的索引组合,例如

newarray=[array1[0],array2[0]];

我得到了算法,但不知道如何循环遍历这两个算法。正如我尝试的那样,但只得到了两者的话。我在 stak overflow 上尝试了 3,4 解决方案,但对我没有用。

最佳答案

如果数组长度相等,您可以只使用 map() 方法循环其中一个并返回包含两个元素的自定义数组:

var results = arr1.map(function(el, index){
return [el, arr2[index]];
});

演示:

var arr1 = ["7@a.com","6@live.com","5@live.com","55@a.com","4@live.com","3@live.com","62@a.com","61@a.com","61@a.com","59@a.com","58@a.com"];

var arr2 = ["5223b14d-1682-4777-8ada-c5b0c972756f","290c79e8-62da-46f1-bff5-157031a079fa","eb4ff5f9-82c7-4095-8116-1c34f1755d06","987c51f4-23f1-4783-8061-a47dfff16fe3","afcf5ef1-a87d-465f-bb1b-f8db81ecc178","2d299d60-9481-4c3f-9b9b-e6659ee74d07","1f40b5ef-990f-4edd-925c-b511dd64899f","88de17fd-286f-4960-9e4f-a2b01425da82","1bb181cb-ab7c-47af-95a6-99357459e6a3","9554f7c2-a20d-4ff7-add6-8840b2d06a89","15e51955-9eb9-4c4b-990f-97e29820e04d"];

var results = arr1.map(function(el, index){
return [el, arr2[index]];
});

console.log(results)

关于javascript - 合并两个数组,使第一个索引都在新数组中,依此类推,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45197235/

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