Menu

gitpiper

initializeNDArray javascript Code Snippet in 2024

arrayrecursionintermediate

Last Updated: 16 June 2024

Create a n-dimensional array with given value.

  • Use recursion.
  • Use Array.from(), Array.prototype.map() to generate rows where each is a new array initialized using initializeNDArray().
const initializeNDArray = (val, ...args) => args.length === 0 ? val : Array.from({ length: args[0] }).map(() => initializeNDArray(val, ...args.slice(1)) );
initializeNDArray(1, 3); // [1, 1, 1] initializeNDArray(5, 2, 2, 2); // [[[5, 5], [5, 5]], [[5, 5], [5, 5]]]

javascript snippet similar to initializeNDArray For You in June 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️