1

Hi I have this example where I want my 1D array to be a 2D array 4x3

var array1 = [15, 33, 21, 39, 24, 27, 19, 7, 18, 28, 30, 38];
var i, j, t;
var positionarray1 = 0;
var array2 = new Array(4);

for (t = 0; t < 4; t++) {
  array2[t] = new Array(3);
}

for (i = 0; i < 4; i++) {
  for (j = 0; j < 3; j++) {
    array2[i][j] = array1[i];
    array2[i][j] = array1[j];
  }

  positionarray1 = positionarray1 + 1; //I do this to know which value we are taking
}

console.log(array2);

My solution is only giving me the first numbers of the array1. Any idea?

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99

4 Answers4

1

i and j are indexes into the new 2D array that only run up to 0 to 3 and 0 to 2, which is why you are seeing the beginning values over and over. You need a way to index array1 that goes from 0 to 11.

It looks like you are on the right track with "positionarray1" and "position", though you need to move where you are incrementing it. You need to use that value when indexing array1 rather than i and j:

    array2[i][j] = array1[positionarray1];

    array2[i][j] = array1[positionarray1];

    positionarray1++;
David Traver
  • 150
  • 5
0

If you rename i to row and j to col, it makes is easier to see what is going on. Also, avoid magic numbers. I am seeing 3 and 4 all over the place. These can be replaced with parameter references. All you need to do it wrap your logic within a reusable function (as seen in the reshape function below).

The main algorithm is:

result[row][col] = arr[row * cols + col];

There is no need to track position, because it can be calculated from the current row and column.

const reshape = (arr, rows, cols) => {
  const result = new Array(rows);
  for (let row = 0; row < rows; row++) {
    result[row] = new Array(cols);
  }
  for (let row = 0; row < rows; row++) {
    for (let col = 0; col < cols; col++) {
      result[row][col] = arr[row * cols + col];
    }
  }
  return result;
};

const array1 = [15, 33, 21, 39, 24, 27, 19, 7, 18, 28, 30, 38];
const array2 = reshape(array1, 4, 3);

console.log(JSON.stringify(array2));
.as-console-wrapper { top: 0; max-height: 100% !important; }
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0
var array1 = [15, 33, 21, 39, 24, 27, 19, 7, 18, 28, 30, 38];
var i, j, t;
var positionarray1 = 0;
var array2 = new Array(4);

for (t = 0; t < 4; t++) {
  array2[t] = new Array(3);
}

for (i = 0; i < 4; i++) {
  for (j = 0; j < 3; j++) {
    array2[i][j] = array1[i*3+j]; //here was the error
  }

  positionarray1 = positionarray1 + 1; //I do this to know which value we are taking
}

console.log(array2);

I just solved it thanks for your comments. I actually used one apportation, but it was 3 instead of 2.

0

solution with 1 loop for efficiency :

const arr1D = new Array(19).fill(undefined).map((_, i) => i);
const arr2D = [];
const cols = 3;

for (let i = 0, len = arr1D.length; i < len; ++i) {
  const col = i % cols;
  const row = Math.floor(i / cols);
  if (!arr2D[row]) arr2D[row] = []; // create an array if not exist
  arr2D[row][col] = arr1D[i];
}

console.log({ arr1D, arr2D });
Joe
  • 59
  • 2