Merge array into a new array javascript in a loop

Hi all i have this loop which will loop 4 times.

for (var i = 0; i < 4; i++){
  var d = [[1111,2222],[3333,4444]]
}

below is my expect output , thank you every much all.

var expected = [ 
 [
   [1111,2222],[3333,4444]
 ], 
 [
   [1111,2222],[3333,4444]
 ], 
 [
   [1111,2222],[3333,4444]
 ], 
 [
   [1111,2222],[3333,4444]
 ]
]

2 answers

  • answered 2022-01-19 16:43 dporechny

    Is that what you tried to achieve?

    const output = [];
    const input = [[1111,2222],[3333,4444]]
    for (var i = 0; i < 4; i++){
      output.push(input)
    }
    console.log(output) // [[[1111,2222],[3333,4444]], [[1111,2222],[3333,4444]], [[1111,2222],[3333,4444]], [[1111,2222],[3333,4444]]]
    

  • answered 2022-01-19 16:44 lucky930

    You can get the result like this

        function getNewArray() {
            var newArray = []
            for (var i = 0; i < 4; i++){
                var d = [[1111,2222],[3333,4444]]
                newArray.push(d)
            }
            return newArray;
        }
        
        let expected = getNewArray()
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum