I am using the faker.js
library to generate random data and I have a couple of factory functions that generate a series of user data:
const createUser = () => {
return {
name: faker.name.findName(),
email: faker.internet.email(),
address: faker.address.streetAddress(),
bio: faker.lorem.sentence(),
image: faker.image.avatar(),
};
};
const createUsers = (numUsers = 5) => {
return Array(numUsers).fill(createUser());
};
let fakeUsers = createUsers(5);
console.log(fakeUsers);
The problem with this Array.fill
approach is that it returns the same data n
number of times. I want 5
different users to be returned from my factory.
How do I do this?
Array.from allows you to create an array and initialize it with values returned from a callback function in one step:
const createUsers = (numUsers = 5) => {
return Array.from({length: numUsers}, createUser);
}