简体   繁体   English

将两个一维数组合并为二维数组javascript

[英]Merge two of one dimensional array into two dimensional array javascript

I have n (but for now, let say just two) of one dimensional arrays like this image of my console :我有n (但现在,假设只有两个)一维数组,例如我的控制台图像:

在此处输入图片说明

And I want to merge these two arrays by the corresponding key and put it into two dimensional array :我想通过相应的键合并这两个数组并将其放入二维数组中:

The result is something like :结果是这样的:

[["1 279 226,08" , "127"],[null , null],["-188 033,77", "154"],..... so on ......]

And the list of one dimensional array is dynamic, it could be more than 2 arrays.并且一维数组的列表是动态的,可以是 2 个以上的数组。

So example if I have 3 arrays, then my two dimensional array would look like :例如,如果我有 3 个数组,那么我的二维数组将如下所示:

[ ["1 279 226,08" , "127" , "blabla"], [null , null , "blabla"], ["-188 033,77", "154", "blabla"], ..... so on ......]

Any ideas of implementing it would be appreciate.任何实施它的想法将不胜感激。

This should do what you want.这应该做你想做的。

 var arr1 = [1, 2, 3, 4, 5, 6]; var arr2 = [6, 5, 4, 3, 2, 1]; var arr3 = [7, 8, 9, 10, 11, 5]; var arr4 = [12, 34, 55, 77, 22, 426]; var arrCollection = [arr1, arr2, arr3, arr4]; // if array sizes are variable. // if not max = arrCollection[0].length will do var max = Math.max.apply(Math, arrCollection.map(function(a) { return a.length; })); var arrFinal = []; for (let i = 0; i < max; i++) { var arr = []; arrCollection.forEach(function(a) { arr.push(a[i]); }); arrFinal.push(arr); } console.log(arrFinal);

You can create this with two forEach() loops.您可以使用两个forEach()循环来创建它。

 var arr1 = [[1, 2], [3, 4], [5, 6]]; var arr2 = [[1, 2, 3], [4, 5, 6], [7, 8], [9, 10, 12, 14]]; let merge = function(arr) { var result = []; arr.forEach(function(e, i) { e.forEach(function(a, j) { if (!result[j]) result[j] = [a]; else result[j].push(a) }) }); return result; } console.log(JSON.stringify(merge(arr1))) console.log(JSON.stringify(merge(arr2)))

You could transpose the array with a nested loop and switch the indices for assigning the values.您可以使用嵌套循环转置数组并切换用于分配值的索引。

 var array = [["1 279 226,08", null, "-188 033,77"], ["127", null, "154"], ["blabla", "blabla", "blabla"]], result = array.reduce(function (r, a, i) { a.forEach(function (b, j) { r[j] = r[j] || []; r[j][i] = b; }); return r; }, []); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

Since all of your arrays have the same size, you can loop just on the lenght of the first array, ie a .由于您的所有数组都具有相同的大小,因此您可以仅在第一个数组的长度上循环,即a

Then we pass to appendArrays the single value of a , b , ..., and we return the an array to push into merged然后我们将ab 、 ... 的单个值传递给appendArrays ,然后我们返回一个数组以推入merged

 var a = ["123", null, "ciao"] var b = ["321", 1, "pippo"] var c = ["111", 5, "co"] var merged = [] for (i = 0; i < a.length; i++) { merged.push(appendArrays(a[i], b[i], c[i])); } console.log(merged); function appendArrays() { var temp = [] for (var i = 0; i < arguments.length; i++) { temp.push(arguments[i]); } return temp; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM