I'm working on a coding exercise where my goal is to write a function, named generatePermutations()
, in NodeJS that takes a single Object
as
an argument. Using all Array
properties in that object, generate an array of
JS objects:
- Each object is a unique permutation of the values found in property arrays.
- A property whose value is an empty array can be ignored.
- If a property in the
Object
argument is not an array then it should be treated as an array containing a single value.
{
pilot: ["Han Solo", "Lando Calrissian"],
copilot: ["Chewbacca", "Rey"],
ship: "Falcon",
speed: "1.5c"
}
The function, in this example, should produce the following permutations:
[
{
"pilot": "Han Solo",
"copilot": "Chewbacca",
"ship": "Falcon",
"speed": "1.5c"
},
{
"pilot": "Han Solo",
"copilot": "Rey",
"ship": "Falcon",
"speed": "1.5c"
},
{
"pilot": "Lando Calrissian",
"copilot": "Chewbacca",
"ship": "Falcon",
"speed": "1.5c"
},
{
"pilot": "Lando Calrissian",
"copilot": "Rey",
"ship": "Falcon",
"speed": "1.5c"
}
]
I've been following this: How can I create all combinations of this object's keys/values in JavaScript? but don't fully understand since I can only pass a single argument.
I'm lost on this one and any pointers would be greatly appreciated!