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?

5
  • Here is an example check stackoverflow.com/a/58138244/5306420 Commented Apr 27, 2021 at 15:51
  • a simple fix would be to change it to array2[i][j] = array1[i*2+j] Commented Apr 27, 2021 at 15:56
  • 1
    The following question is related and contains various strategies: "Convert simple array into two-dimensional array (matrix) ". Commented Apr 27, 2021 at 16:01
  • This is Javascript, not Java. Please don't tag Javascript questions with [java]. Commented Apr 27, 2021 at 22:05
  • I didn't do that haha! Commented Apr 28, 2021 at 23:24

4 Answers 4

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++;
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

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 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.