Since you're iterating for the sole purpose of creating an array, map
is somewhat appropriate here.
const productArr = [... Array(dataLength)] // Create a dummy array dataLength elements long
.map(_ => ({ // And map over it
name: faker.commerce.productName(),
category: faker.commerce.department(),
price: faker.commerce.price(),
description: faker.lorem.paragraph(),
}));
I'm mentioning it just in case you aren't aware of map
, but it could be argued that this exact usage is an abuse of the function. Really, map
is for transforming one list into another list of the same length. In this case though, we're completely ignoring the original contents of the array being iterated over (shown by the fact that the parameter is called _
).
I can't say I necessarily recommend map
in this case, but I thought I'd mention it.
If however you ever wanted to enumerate the data you're producing, it would be appropriate:
const productArr = [... Array(dataLength).keys()] // [0, 1, 2, 3, ...]
.map(i => ({
i: i, // And use the parameter this time
name: faker.commerce.productName(),
category: faker.commerce.department(),
price: faker.commerce.price(),
description: faker.lorem.paragraph(),
}));
Resource for the "range" on the first line..