Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

javascript - Faker.js random word with choices

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?

question from:https://stackoverflow.com/questions/66051075/faker-js-random-word-with-choices

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...