Skip to content

Option to create random array of objects or arrays #1549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nicolasWD opened this issue Nov 11, 2022 · 2 comments
Closed

Option to create random array of objects or arrays #1549

nicolasWD opened this issue Nov 11, 2022 · 2 comments
Labels
has workaround Workaround provided or linked question Further information is requested

Comments

@nicolasWD
Copy link

Clear and concise description of the problem

I need to create an array of objects with a random number of elements.

At the moment I am using Array.fill, followed by Array.map using faker.mersenne.rand to randomize the number of elements of my array.

Suggested solution

Is there a way faker could simplify this instead ?

Alternative

No response

Additional context

No response

@nicolasWD nicolasWD added the s: pending triage Pending Triage label Nov 11, 2022
@ST-DDT
Copy link
Member

ST-DDT commented Nov 11, 2022

This is planned as a new feature in v8: #1545 + #1486

faker.helpers.multiple(method: () => T, { count:  number | { min: number, max: number }});

faker.helpers.multiple(faker.datatype.number, { count: { min: 5, max: 10 }});
// [ 45614, 684631, 468435, 634639, 95460, 340]
faker.helpers.multiple(() => faker.datatype.number({min: 0, max: 1337}), { count: { min: 5, max: 42 }});
// [ 643, 468, 51, 10, 183, ... ]

Current workaround:

const length = faker.datatype.number({ min: 5, max: 10 });
return Array.from({ length }, () => 
  faker.datatype.number({min: 0, max: 1337})
);

@ST-DDT ST-DDT added the has workaround Workaround provided or linked label Nov 11, 2022
@Shinigami92 Shinigami92 added the question Further information is requested label Nov 11, 2022
@nicolasWD
Copy link
Author

This is planned as a new feature in v8: #1545 + #1486

faker.helpers.multiple(method: () => T, { count:  number | { min: number, max: number }});

faker.helpers.multiple(faker.datatype.number, { count: { min: 5, max: 10 }});
// [ 45614, 684631, 468435, 634639, 95460, 340]
faker.helpers.multiple(() => faker.datatype.number({min: 0, max: 1337}), { count: { min: 5, max: 42 }});
// [ 643, 468, 51, 10, 183, ... ]

Current workaround:

const length = faker.datatype.number({ min: 5, max: 10 });
return Array.from({ length }, () => 
  faker.datatype.number({min: 0, max: 1337})
);

Thank you, I will be waiting for the multiple method! Your workaround is much better for now!

@xDivisionByZerox xDivisionByZerox removed the s: pending triage Pending Triage label Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has workaround Workaround provided or linked question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants