合并两个数组,以便第一个索引都在新数组中,依此类推

时间:2017-07-19 17:25:36

标签: javascript jquery arrays loops

我有两个像这样的数组

第一个阵列是

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]];

我得到了算法,但我不知道如何在两者上循环。正如我尝试但只得到两者的话。我尝试了3,4溢出溢出但是对我没有用。

3 个答案:

答案 0 :(得分:4)

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

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

<强>演示:

&#13;
&#13;
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)
&#13;
&#13;
&#13;

答案 1 :(得分:3)

您可以将它们映射为多对值:

&#13;
&#13;
var array1 = ["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 array2 = ["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 result = array1.map(function(v1, i) {
  return [v1, array2[i]];
});

console.log(result);
&#13;
&#13;
&#13;

答案 2 :(得分:1)

array1 = [ "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"]; array2 = [
"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 newArray = [];
for (var i=0; i<array1.length; i++)
    newArray.push([ array1[i], array2[i]]);
console.log( newArray );