从1d数组创建2d数组

时间:2022-01-12 02:20:38

Im new to programming, I have an assignment that asks to create a 2d-array from a 1d array. I came up with this (no help from any outside sources because it takes away the learning experience). It works for our professors test inputs, I was just wondering is this an ugly/inefficient solution.

我是编程新手,我有一个任务,要求从1d数组创建一个二维数组。我想出了这个(没有任何外部来源的帮助,因为它带走了学习经验)。它适用于我们的教授测试输入,我只是想知道这是一个丑陋/低效的解决方案。

function twoDArray(arr, lenSubArray) {
    var newArr = []; 
    var placeHolder = 0; 
    var leftOver = 0; 
    for (var i = 1; i < arr.length + 1; i++) {
        /* if i is a multiple of the specified sub-array size 
           then add the elements from placeHolder to i
        */
        if (i % lenSubArray === 0) {
            newArr.push(arr.slice(placeHolder, i)); 
            placeHolder += lenSubArray; 
            leftOver++; // tells us how many sub-arrays were created
        }
    }
    /* if original array is not divisible by the length of the specified sub-array
       then there will be left over values. Retrieve these values and create an 
       array out of them and add them to the 2d array.
    */
    if (!(arr.length % lenSubArray === 0)) {
         /* sub-array count multiplied by the length of each 
            sub-array gives us the right index to retrieve remaining values
        */
        leftOver = (leftOver * lenSubArray);
        newArr.push(arr.slice(leftOver))
    }

    return newArr; 
}

Test input: twoDArray([1, 2, 3, 4, 5], 3) output would be: [[1, 2, 3], [4, 5]]

测试输入:twoDArray([1,2,3,4,5],3)输出将是:[[1,2,3],[4,5]]

3 个解决方案

#1


1  

you're way too complicated:

你太复杂了:

  • create a result-array
  • 创建一个结果数组

  • push slices from i to i+lenSubArray
  • 将切片从i推送到i + lenSubArray

  • increment i by lenSubArray
  • 通过lenSubArray递增i

and slice is smart enough to properly handle the end of the Array

和slice足够聪明,可以正确处理数组的结尾

function twoDArray(arr, lenSubArray) {
  var i = 0, result = [];
  while(i < arr.length)
    result.push( arr.slice(i, i+=lenSubArray) );
  return result;  
}

#2


1  

You could use Array#reduce and build new arrays based on the index.

您可以使用Array#reduce并根据索引构建新数组。

function twoDArray(array, length) {
    return array.reduce(function (r, a, i) {
        i % length ? r[r.length - 1].push(a) : r.push([a]);
        return r;
    }, []);
}

console.log(twoDArray([1, 2, 3, 4, 5], 3));

#3


0  

You do not need to iterate over an array. You can use Array.prototype.slice function instead.

您不需要遍历数组。您可以使用Array.prototype.slice函数。

function twoDArray(a, b){
    return (Array(Math.ceil(a.length / b)) + '').split(',').map(function(c, d){
        return a.slice(b * d, b * (d + 1));
    });
}

Here is how you call it

这就是你怎么称呼它

var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
console.log(twoDArray(a, 3));
// Output:
// [
//   [1, 2, 3],
//   [4, 5, 6],
//   [7, 8, 9],
//   [10, 11, 12]
// ]

#1


1  

you're way too complicated:

你太复杂了:

  • create a result-array
  • 创建一个结果数组

  • push slices from i to i+lenSubArray
  • 将切片从i推送到i + lenSubArray

  • increment i by lenSubArray
  • 通过lenSubArray递增i

and slice is smart enough to properly handle the end of the Array

和slice足够聪明,可以正确处理数组的结尾

function twoDArray(arr, lenSubArray) {
  var i = 0, result = [];
  while(i < arr.length)
    result.push( arr.slice(i, i+=lenSubArray) );
  return result;  
}

#2


1  

You could use Array#reduce and build new arrays based on the index.

您可以使用Array#reduce并根据索引构建新数组。

function twoDArray(array, length) {
    return array.reduce(function (r, a, i) {
        i % length ? r[r.length - 1].push(a) : r.push([a]);
        return r;
    }, []);
}

console.log(twoDArray([1, 2, 3, 4, 5], 3));

#3


0  

You do not need to iterate over an array. You can use Array.prototype.slice function instead.

您不需要遍历数组。您可以使用Array.prototype.slice函数。

function twoDArray(a, b){
    return (Array(Math.ceil(a.length / b)) + '').split(',').map(function(c, d){
        return a.slice(b * d, b * (d + 1));
    });
}

Here is how you call it

这就是你怎么称呼它

var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
console.log(twoDArray(a, 3));
// Output:
// [
//   [1, 2, 3],
//   [4, 5, 6],
//   [7, 8, 9],
//   [10, 11, 12]
// ]