How to declare an empty two dimensional (2D) array in Node.js

2 Answers

0 votes
const rows = 3;
const cols = 2;
  
const arr = [...Array(rows)].map(x=>Array(cols).fill());  
  
console.log(arr); 
  
    
    
      
      
/*
run:
      
[
  [ undefined, undefined ],
  [ undefined, undefined ],
  [ undefined, undefined ]
]

*/

 



answered May 23, 2022 by avibootz
0 votes
const rows = 3;
const cols = 2;
  
const arr = [...Array(rows)].map(x=>Array(cols).fill(0));  
  
console.log(arr); 
  
    
    
      
      
/*
run:
      
[ [ 0, 0 ], [ 0, 0 ], [ 0, 0 ] ]

*/

 



answered May 23, 2022 by avibootz
...