skip to Main Content

I’m working on a project where I need to create an empty copy of a 2D array in JavaScript. I have an original array filled with the number of days in a month:

var originalArray = [
    [1, 2, 3, 4, 5, 6, 7], 
    [8, 9, 10, 11, 12, 13, 14], 
    [15, 16, 17, 18, 19, 20, 21], 
    [22, 23, 24, 25, 26, 27, 28], 
    [29, 30, 31]
];

Expected result:

copiedArray = [
    [, , , , , , ], 
    [, , , , , , ], 
    [, , , , , , ], 
    [, , , , , , ], 
    [, , ]
];

How do I create a new empty array with the exact number of elements as the original array?
Thanks in advance!

I’ve tried a nested loop but not successful.

5

Answers


  1. something like this?

    var originalArray = [
        [1, 2, 3, 4, 5, 6, 7], 
        [8, 9, 10, 11, 12, 13, 14], 
        [15, 16, 17, 18, 19, 20, 21], 
        [22, 23, 24, 25, 26, 27, 28], 
        [29, 30, 31]
    ];
    
    const copiedArray = originalArray.map(a => Array(a.length).fill())
    
    console.log(copiedArray)
    Login or Signup to reply.
  2. maybe you can try this ?

    var copiedArray = new Array(originalArray.length);
    for (var i = 0; i < originalArray.length; i++) {
        copiedArray[i] = new Array(originalArray[i].length);
    }
    

    if you want to make it recursive you can also do it like this :

    function createEmptyCopy(array) {
      var copiedArray = new Array(array.length);
      
      for (var i = 0; i < array.length; i++) {
        if (Array.isArray(array[i])) {
          copiedArray[i] = createEmptyCopy(array[i]);
        } else {
          copiedArray[i] = undefined;
        }
      }
      
      return copiedArray;
    }
    
    Login or Signup to reply.
  3.     let newArray = originalArray.map(row => row.map(column => ""))
    

    The line above seems to do the job like you said. If you want to change the empty string to null or undefined, it will still work without any issues. You can run an example below to see whether it is upto your specs or not.

    let originalArray = [
        [1, 2, 3, 4, 5, 6, 7], 
        [8, 9, 10, 11, 12, 13, 14], 
        [15, 16, 17, 18, 19, 20, 21], 
        [22, 23, 24, 25, 26, 27, 28], 
        [29, 30, 31]
    ];
    
    let newArray = originalArray.map(row => row.map(column => ""))
    
    console.log(newArray)
    Login or Signup to reply.
  4. like this

    const originalArray = [
        [1, 2, 3, 4, 5, 6, 7], 
        [8, 9, 10, 11, 12, 13, 14], 
        [15, 16, 17, 18, 19, 20, 21], 
        [22, 23, 24, 25, 26, 27, 28], 
        [29, 30, 31]
    ];
    
    const copiedArray = originalArray.map(row => {
      return new Array(row.length);
    });
    
    console.log( copiedArray );
    Login or Signup to reply.
  5. You can use Array.from and fill() like this:

    const original = [
        [1, 2, 3, 4, 5, 6, 7],
        [8, 9, 10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19, 20, 21],
        [22, 23, 24, 25, 26, 27, 28],
        [29, 30, 31]
    ];
    
    const copied = Array.from(original, el => Array(el.length).fill());
    
    console.log(copied);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search