javascriptnode.jsinquirer

Is there a way to use an array of objects to populate choices in inquirer?


How do I get the choices to be just a specific value from all objects in the array?

Here's an example array:

var array = 
[{
  a: 1,
  b: 2,
},
{
 a: 3,
 b: 4,
},
{
  a: 5,
  b: 6,
}]

And then later the inquirer is something like:

inquirer
.prompt({
  name: "test",
  type: "list",
  message: "Example Question",
  choices: [{array.b}]
})

My desired result would be the following as inquirer options for the list question:

[2,4,6]

Solution

  • If you want to get the b values only you can use lodash map and iterator methods to achieve that. Ex:

    _.map(array, _.iteratee('b'); // [2,4,6]