I'm trying to figure out how to convert the following javascript function into a dynamic function that will perform the flapMap recursively.
function getPermutations(object) {
let array1 = object[0].options,
array2 = object[1].options,
array3 = object[2].options;
return array1.flatMap(function(array1_item) {
return array2.flatMap(function(array2_item) {
return array3.flatMap(function(array3_item) {
return array1_item + ' ' + array2_item + ' ' + array3_item;
});
});
});
}
let object = [{
"options": ['blue', 'gray', 'green']
}, {
"options": ['large', 'medium', 'small']
}, {
"options": ['wood', 'steel', 'pastic']
}];
console.log('Permutations', getPermutations(object));
In the example, I'm sending 3 arrays into the function which is why it has 3 iterations of flapMap. Works fine, but I am trying to make it dynamic, so I can pass a dynamic array and the function would do the flapMap recursively depending on the array.
In your example you kept track of array1_item, array2_item and co in their individual variables. You can move them to an array (having dynamic size; I called it _prevItems) and pass them as an parameter to the recursive call.
function getPermutations(objects, _prevItems = []) {
// join the items at the end of the recursion
if (objects.length === 0)
return _prevItems.join(' ')
// call again with all but the first element, and add the current item to _prevItems
return objects[0].flatMap(item => getPermutations(objects.slice(1), [..._prevItems, item]))
}
let objects = [['blue', 'gray', 'green'], ['large', 'medium', 'small'], ['wood', 'steel', 'pastic']];
console.log('Permutations', getPermutations(objects));
One way to do this is with reduce.
You want to reduce the list of options to one list of permutations.
function getPermutations(list) {
return (
list
// First map to list of options (list of list of strings)
.map((item) => item.options)
// Then reduce. Do not set any initial value.
// Then the initial value will be the first list of options (in
// our example, ["blue", "gray", "green"])
.reduce((permutations, options) => {
return permutations.flatMap((permutation) =>
options.map((option) => permutation + " " + option)
);
})
);
}
// Renamed this to list, since it is an array and not an object
const list = [
{ options: ["blue", "gray", "green"] },
{ options: ["large", "medium", "small"] },
{ options: ["wood", "steel", "pastic"] },
];
console.log("Permutations", getPermutations(list));
Edit: I know you asked for recursion. If this is a school task, then perhaps you have to use recursion, but otherwise I would recommend avoiding recursion when possible, since it tends to make things more complicated. (Of course, this is a general rule, and like all rules it has some exceptions.)
you can flatMap two arrays at a time using the recursive bottom up approach and build your string from the end
const getPermutations = (array) => {
if(array.length === 1)
return array[0].options;
const prefixItems = array[0].options;
const suffixItems = getPermutations(array.slice(1));
return prefixItems.flatMap(prefix => {
return suffixItems.flatMap(suffix => {
return prefix + ' ' + suffix
});
})
}