If I have an array like [["a", "b"], ["c", "d"]]
, is there a way to iterate, reduce, map, or join this array in order to get the desired output of ["ac", "ad", "bc", "bd"]
? What if the array is structured as
[["a", "b"], ["c", "d"], ["e", "f"]]
; how can we then obtain ["ace", "acf", "ade", "adf", "bce", "bcf", "bde", "bdf"]
?
Is it possible to achieve this using array iteration methods?
I attempted to use the reduce method:
const output = [];
const solution = array.reduce((cumulative, individual) => {
for (let i = 0; i <= cumulative.length; i++) {
for (let j = 0; j <= individual.length; j++) {
output.push(`${cumulative[i]} + ${individual[j]}`);
}
}
});
console.log(output);
However, my code did not produce the exact desired output.