Per-column arrays must each be 1-dimensional

Error: per-column arrays must each be 1-dimensional

This error occurs when working with multidimensional arrays in a way that violates the requirement that each column array must only have one dimension.

Let’s understand this error with an example:

const array = [[1, 2, 3], [4, 5, 6]];
const invalidArray = [[1, 2, 3], [4, 5, 6, 7]];

console.log(array[0]);  // [1, 2, 3]
console.log(array[0].length);  // 3

console.log(invalidArray[1]);  // [4, 5, 6, 7]
console.log(invalidArray[1].length);  // 4

In the above example, we have two arrays: array and invalidArray.

The array is a valid 2D array with 2 rows and each row having 3 elements. Accessing array[0] gives us the first row, which is a 1-dimensional array [1, 2, 3].

The invalidArray is also a 2D array with 2 rows, but the second row violates the requirement. It has 4 elements instead of 3. Accessing invalidArray[1] gives us the second row, which is a 1-dimensional array [4, 5, 6, 7].

To fix this error, ensure that all column arrays in your multidimensional array have the same length or dimension.

Related Post

Leave a comment