I have an array of arrays, and when I iterate through them with foreach, I try to call flatmap on the items, but js says flatmap is not a function
var arrayItems = [];
arrayItems.push(["a", "b", "c", "d", "e"]);
arrayItems.push(["c", "d", "e", "f"]);
arrayItems.push(["c", "e", "f", "g", "h"]);
arrayItems.push(["e", "f", "g", "h"]);
var variations = generate_variations(arrayItems);
function generate_variations(arrayItems) {
var variations = [];
//https://stackoverflow.com/questions/43241174/javascript-generating-all-combinations-of-elements-in-a-single-array-in-pairs/43241287
arrayItems.forEach(([key, value]) =>
variations.push(value.flatMap(
(v, i) => value.slice(i + 1).map(w => [v, w])
)));
return variations;
}
console.log(variations);
Here is a simple flatmap that works fine (I tried to place this in a for loop) from Javascript - Generating all combinations of elements in a single array (in pairs)
var array = ["apple", "banana", "lemon", "mango"];
var result = array.flatMap(
(v, i) => array.slice(i + 1).map(w => [v, w])
);
console.log(result);