-5

I wanted to make an array inside another array.

[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]

a bit like this. So far all I've got is

     var row = new Array(4);
     var row1 = [1,2,3,4];
     for (var i = 0; i< row1.length; i++ {
         row[i] = new Array(3);  // enter code here
     } 
     console log(row);

but I only receive empty items and I'm not sure what I'm doing wrong.

Milkncookiez
  • 6,817
  • 10
  • 57
  • 96

2 Answers2

2

Use .push() to push array inside the original array

var row = [];
     var row1 = [1,2,3,4];
     for (var i = 0; i< row1.length; i++) {
     row.push(row1);
     } 
     console.log(JSON.stringify(row));
ellipsis
  • 12,049
  • 2
  • 17
  • 33
  • **Note** If you mutate any of the inner array (*say row[0].push(5)*), it will update all entries – Rajesh Feb 07 '19 at 12:44
1

Try

 let row= [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]];
 console.log(row);

or this

let row = [1,2,3,4].map(x=>[1,2,3,4]);
console.log(row);
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
  • This is not FB or 9gag. – Rajesh Feb 07 '19 at 12:45
  • 1
    @Rajesh yet when OP is obviously trying to offload homework (or at the very least, does not seem to have put in any effort at attempting the task or formulating a question for that matter), there's not much to be done. Also please have a look at [this](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) and note that the top answer is a community wiki. – ManavM Feb 07 '19 at 12:53
  • 1
    I know and can't agree more. However all we can do is downvote post and move on. If you still wish to answer, at least make sure it justifies the term **answer**. This is what I believe. Others will have different views. So I do not entertain such posts much – Rajesh Feb 07 '19 at 12:56