During a coding test, I wanted to create an array of N numbers and use forEach
.
While I could have used a simple for
or while
loop, I was more comfortable using array methods, so I started searching for a suitable method.
1D Array (Array of N numbers)
- for loop
function createArray(n) {
const result = []
for (let i = 1; i <= n; i++) {
result.push(i)
}
return result
}
- while loop
function createArray(n) {
const result = []
let i = 1
while (i <= n) {
result.push(i)
i++
}
return result
}
- Array.from
//* Recommend
const array = Array.from({ length: n }, (_, i) => i + 1)
- Array.map
const array = Array(n)
.fill()
.map((_, i) => i + 1)
Creating a 2D Array, e.g. [[1], [1,2], [1,2,3], [1,2,3,4], [1,2,3,4,5]]
- for loop
function createArrays(n) {
const result = []
for (let i = 1; i <= n; i++) {
const arr = []
for (let j = 1; j <= i; j++) {
arr.push(j)
}
result.push(arr)
}
return result
}
- while loop
function createArrays(n) {
const result = []
let i = 1
while (i <= n) {
const arr = []
let j = 1
while (j <= i) {
arr.push(j)
j++
}
result.push(arr)
i++
}
return result
}
- Array.from
//* Recommend
function createArrays(n) {
return Array.from({ length: n }, (_, i) =>
Array.from({ length: i + 1 }, (_, j) => j + 1)
)
}
- Array.map
function createArrays(n) {
return Array(n)
.fill()
.map((_, i) =>
Array(i + 1)
.fill()
.map((_, j) => j + 1)
)
}
Creating an n x n 2D Array
Array.from({ length: n }, () => Array(n).fill(0))