Use JavaScript or Lodash to create multi dimensional array

  • A+
Category:Languages

I want to create a multi-dimensional array like this using Lodash or vanilla JS:

[   [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],   [11, 12, 13, 14, 15, 16, 17, 18, 19, 20],   etc ] 

This is a simplistic example since I want this pattern to continue up to 1,000,000 but for demonstration 1 to 20 is fine.

Any ideas? I've tried _.range(20) so far but I need this Array to be multi-dimensional. Thanks

 


With lodash you can use chunk:

const result = _.chunk(_.range(1, 21), 10); console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: