0

I'm looking for an implementation of createEmpties(nValues) as below:

var result = createEmpties(4);
console.log(result)

[[], [], [], []]

I tried using map:

new Array(nValues).map(_ => []);

but got this output:

[ <4 empty items> ]

heltonbiker
  • 26,657
  • 28
  • 137
  • 252

3 Answers3

4

You can do it using Array.from.

const result = Array.from({ length: 4 }, () => []);
console.log(result);
Derek Wang
  • 10,098
  • 4
  • 18
  • 39
  • Interesting difference> `const result = Array.from({ length: 4 }, () => []); result[2].push("hello"); console.log(result); const result1 = new Array(4).fill([]); result1[2].push("hello"); console.log(result1); ` – mplungjan Oct 06 '20 at 18:21
  • Derek - I've just been reading [using fill to create array](https://stackoverflow.com/questions/64230222/javascript-pushing-in-an-empty-3d-array), could your method be used to create the array in that question? – ATD Oct 06 '20 at 18:23
  • `Array.fill` fills object reference not creating new instance. – Derek Wang Oct 06 '20 at 18:25
  • @ATD It could yes, as explained in [this answer](https://stackoverflow.com/a/43294613). (I've just voted to close the one you mentioned as a duplicate of it.) – Ivar Oct 06 '20 at 18:29
  • https://stackoverflow.com/a/64230402/8202850, this explains about `Array.fill`. – Derek Wang Oct 06 '20 at 18:30
  • Thanks - I've managed to put something together using your method that allows me to create a 2d array of arrays of any size that can be populated using simple array[x][y].push(n) syntax – ATD Oct 06 '20 at 18:57
  • This is slower than fill and map on Chrome v80. https://stackoverflow.com/questions/35578478/array-prototype-fill-with-object-passes-reference-and-not-new-instance – jens Oct 09 '20 at 06:33
2

The new Array, will not fill. Try Array(nValues).fill(null).map(_ => []). This should give you what you are looking for.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill

jens
  • 2,075
  • 10
  • 15
  • 2
    you should see this: [fill array](https://stackoverflow.com/questions/64230222/javascript-pushing-in-an-empty-3d-array) Using fill can cause problems. The recommendation there seems to be to use map instead of fill. – ATD Oct 06 '20 at 18:17
  • @ATD - interesting result: `const result = Array.from({ length: 4 }, () => []); result[2].push("hello"); console.log(result); const result1 = new Array(4).fill([]); result1[2].push("hello"); console.log(result1); ` – mplungjan Oct 06 '20 at 18:22
  • @mplungjan Yep - that was what led to [create array using fill](https://stackoverflow.com/questions/64230222/javascript-pushing-in-an-empty-3d-array) The link to mozilla above actually mentions that (though not entirely clearly) as the last point in the Description section. – ATD Oct 06 '20 at 18:25
-1

Here's a function that gives you exactly what you asked for:

function createEmpties(n) {
    return Array(n).fill(new Array());
}

var result = createEmpties(4);
console.log(result)
CryptoFool
  • 21,719
  • 5
  • 26
  • 44
Gopi krishna
  • 308
  • 1
  • 9
  • Have *you* tried this? It won't do what's presumably expected. – Scott Sauyet Oct 06 '20 at 18:29
  • 1
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Matt D Oct 06 '20 at 20:30