If the challenge/problem specified the cardinality of the sets each of the dimensions span, we could use a recursive function, something like:
type Space<T> = T | Array<Space<T>> function initSpace<T>(identity: T, ...cardinalities: number[]): Space<T> { if (!cardinalities.length) return identity let cardinality = cardinalities[0] cardinalities = cardinalities.slice(1) return new Array(cardinality) .fill(null) .map(() => initSpace(identity, ...cardinalities)) }
Let's say you want that three-dimensional array to be Rubik's cube-like, filled with zeros:
let numbers: Space<number> = initSpace<number>(0, 3, 3, 3)
Otherwise, I'd simply go for the solution @wingkwong posted: [] π€·ββοΈ
[]
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
If the challenge/problem specified the cardinality of the sets each of the dimensions span, we could use a recursive function, something like:
Let's say you want that three-dimensional array to be Rubik's cube-like, filled with zeros:
Otherwise, I'd simply go for the solution @wingkwong posted:
[]
π€·ββοΈ