10

I would like to create a two-dimensional array that gets initialized with booleans which are set to false. Currently I'm using this method of array creation:

const rows = 3
const cols = 5

const nestedArray = new Array(rows).fill(
    new Array(cols).fill(false)
)

The nestedArray looks fine, but as soon as I change the value of nestedArray[0][2], the values of nestedArray[1][2] and nestedArray[2][2] also get changed.

I guess this is because the sub-arrays are identical, probably because they get filled into the parent array by reference and not by value.

What would be an elegant and efficient way of creating an array of non-identical sub-arrays instead?

2

6 Answers 6

10

You can use nested Array.from() calls:

const rows = 3
const cols = 5

const nestedArray = Array.from({ length: rows }, () => 
  Array.from({ length: cols }, () => false)
);
  
nestedArray[0][1] = 'value'; // example of changing a single cell
  
console.log(nestedArray);

Sign up to request clarification or add additional context in comments.

Comments

5

You could use Array.from method to create rows where second parameter is map method, and Array.fill for columns.

const rows = 3
const cols = 5

const nestedArray = Array.from(Array(rows), _ => Array(cols).fill(false));
nestedArray[0][1] = true;
console.log(nestedArray)

Another approach would be to use spread syntax ... on rows array so you can then use map method on that array.

const rows = 3
const cols = 5

const nestedArray = [...Array(rows)].map(_ => Array(cols).fill(false))
nestedArray[0][1] = true;
console.log(nestedArray)

Comments

1

Quite similar to Ori Drori answer, but a little bit shorter :

const rows = 3;
const cols = 5;

const nestedArray = Array.from({length: rows}, () => Array(cols).fill(false));
nestedArray[1][2] = true;
console.log(nestedArray);

Comments

0
const nestedArray = Array(rows).fill(false).map(x => Array(cols).fill(false))

try this one

1 Comment

While this answer is probably correct and useful, it is preferred if you include some explanation along with it to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and users need to understand how it once worked.
0

Maybe something like:

const rows = 3
const cols = 5

const nestedArray = new Array(rows).fill(0);

nestedArray.forEach((e, i, a) => a[i] = new Array(cols).fill(false));

console.log(nestedArray);

  • First, initialize the array with dummy values
  • Then, for each position initialize another nested array

Comments

0

There are already a lot of answers but maybe someone find this one more readable:

let rows = 3;
let columns = 5;
let rowTemplate = Array(columns).fill(false);
let matrix = Array.from(Array(rows), () => [...rowTemplate]);

matrix[1][3] = true;
/*
[
  [ false, false, false, false, false ],
  [ false, false, false, true, false ],
  [ false, false, false, false, false ]
]
*/

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.