如何将两个数组连接成一个二维数组?

时间:2023-01-14 12:12:03

I have two arrays. How can I join them into one multidimensional array?

我有两个数组。如何将它们连接成一个多维数组?

The first array is:

第一个数组是:

var arrayA = ['Jhon, kend, 12, 62626262662', 
              'Lisa, Ann, 43, 672536452', 
              'Sophie, Lynn, 23, 636366363'];

My other array has the values:

我的另一个数组有值:

var arrayB = ['Jhon', 'Lisa', 'Sophie']; 

How could I get an array with this format??

我怎么能得到这种格式的数组?

var jarray = [['Jhon', ['Jhon, kend, 12, 62626262662']], 
              ['Lisa', ['Lisa, Ann, 43, 672536452']], 
              ['Sohphie', ['Sophie, Lynn, 23, 636366363']]]

4 个解决方案

#1


4  

var jarray = [];
for (var i=0; i<arrayA.length && i<arrayB.length; i++)
    jarray[i] = [arrayB[i], [arrayA[i]]];

However, I wouldn't call that "multidimensional array" - that usually refers to arrays that include items of the same type. Also I'm not sure why you want the second part of your arrays be an one-element array.

但是,我不会称之为“多维数组” - 通常是指包含相同类型项的数组。此外,我不确定为什么你希望你的数组的第二部分是一个单元素数组。

#2


0  

You can use Underscore.js http://underscorejs.org/#find Looks through each value in the list, returning the first one that passes a truth test (iterator). The function returns as soon as it finds an acceptable element, and doesn't traverse the entire list.

你可以使用Underscore.js http://underscorejs.org/#find查看列表中的每个值,返回第一个通过真值测试(迭代器)的值。该函数在找到可接受的元素后立即返回,并且不会遍历整个列表。

var even = _.find([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
=> 2

Then, you can make the same with the array B elements and by code, make a join.

然后,您可以使用数组B元素和代码进行相同的连接。

#3


0  

This is what I did to get what you were asking:

这就是我为了得到你所要求的而做的事情:

var jarray = [];
for (var i = 0; i < arrayB.length; i++) {
    jarray[i] = [];
    jarray[i].push(arrayB[i]);
    var valuesList = [],
        comparator = new RegExp(arrayB[i]);
    for (var e = 0; e < arrayA.length; e++) {       
        if (comparator.test(arrayA[e])) {
            valuesList.push(arrayA[e]);
       }        
    }
    jarray[i].push(valuesList);
}

#4


0  

Here is a map version

这是一个地图版本

var arrayA = ['Jhon, kend, 12, 62626262662', 
              'Lisa, Ann, 43, 672536452', 
              'Sophie, Lynn, 23, 636366363'];

var arrayB = ['Jhon', 'Lisa', 'Sophie']; 

/*  expected output
    var jarray = [['Jhon', ['Jhon, kend, 12, 62626262662']], 
                 ['Lisa', ['Lisa, Ann, 43, 672536452']], 
                 ['Sohphie', ['Sophie, Lynn, 23, 636366363']]] */
              
 var jarray = arrayB.map(function(item,i) { 
   return [item,[arrayA[i]]]; 
 });
 console.log(jarray);

#1


4  

var jarray = [];
for (var i=0; i<arrayA.length && i<arrayB.length; i++)
    jarray[i] = [arrayB[i], [arrayA[i]]];

However, I wouldn't call that "multidimensional array" - that usually refers to arrays that include items of the same type. Also I'm not sure why you want the second part of your arrays be an one-element array.

但是,我不会称之为“多维数组” - 通常是指包含相同类型项的数组。此外,我不确定为什么你希望你的数组的第二部分是一个单元素数组。

#2


0  

You can use Underscore.js http://underscorejs.org/#find Looks through each value in the list, returning the first one that passes a truth test (iterator). The function returns as soon as it finds an acceptable element, and doesn't traverse the entire list.

你可以使用Underscore.js http://underscorejs.org/#find查看列表中的每个值,返回第一个通过真值测试(迭代器)的值。该函数在找到可接受的元素后立即返回,并且不会遍历整个列表。

var even = _.find([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
=> 2

Then, you can make the same with the array B elements and by code, make a join.

然后,您可以使用数组B元素和代码进行相同的连接。

#3


0  

This is what I did to get what you were asking:

这就是我为了得到你所要求的而做的事情:

var jarray = [];
for (var i = 0; i < arrayB.length; i++) {
    jarray[i] = [];
    jarray[i].push(arrayB[i]);
    var valuesList = [],
        comparator = new RegExp(arrayB[i]);
    for (var e = 0; e < arrayA.length; e++) {       
        if (comparator.test(arrayA[e])) {
            valuesList.push(arrayA[e]);
       }        
    }
    jarray[i].push(valuesList);
}

#4


0  

Here is a map version

这是一个地图版本

var arrayA = ['Jhon, kend, 12, 62626262662', 
              'Lisa, Ann, 43, 672536452', 
              'Sophie, Lynn, 23, 636366363'];

var arrayB = ['Jhon', 'Lisa', 'Sophie']; 

/*  expected output
    var jarray = [['Jhon', ['Jhon, kend, 12, 62626262662']], 
                 ['Lisa', ['Lisa, Ann, 43, 672536452']], 
                 ['Sohphie', ['Sophie, Lynn, 23, 636366363']]] */
              
 var jarray = arrayB.map(function(item,i) { 
   return [item,[arrayA[i]]]; 
 });
 console.log(jarray);