Reputation: 31
I'm trying to use faker.js to generate some random words, but I want it to choose between a few choices.
faker.random.word()
I tried something like this
faker.random.word({constraints: ['','','']})
This didn't work, and I could be way off since this was just a straight up guess. I tried looking for documentation on it but couldn't find any. Any suggestions?
Upvotes: 2
Views: 5570
Reputation: 3829
You can generate a number between 0 and the number of words you have and access the words using an array
const words = ["foo", "bar", "baz"]
const randomNumber = faker.datatype.number({
'min': 0,
'max': words.length - 1
});
console.log(words[randomNumber])
Upvotes: 3
Reputation: 11
i was looking for same issue in php , the php way is this :
echo $faker->randomElements(['a', 'b', 'c', 'd', 'e']);
['c']
Upvotes: 0
Reputation: 5051
I have not seen anything like this in faker module but you can using uniqueNamesGenerator
module, check the documentation
const { uniqueNamesGenerator } = require('unique-names-generator'); ;
const colors = [
'Green', 'Red', 'Yellow', 'Black'
]
const characterName = uniqueNamesGenerator({
dictionaries: [colors],
length: 1
});
console.log(characterName)
Upvotes: 0