[enter image description here][1]for example
var arr1 = ['A','B','C'];
var arr2 = [['D','E','F']['G','H','I']]
the outcome should be:
['ADG','ADH','ADI','AEG','AEH','AEI','AFG','AFH','AFI','BDG','BDH','BDI'.....]
but it should also be dynamic so that if another array is added to arr2 it should pick that as well
below is a picture of how its being implemented currently [1]: https://i.stack.imgur.com/JtfD9.png
CodePudding user response:
The fact that you have those two different array variables is immaterial. Could just throw everything together1 and use reduceRight()
:
const arrays = [['A','B','C'], ['D','E','F'], ['G','H','I']];
const combine = (arrays) =>
arrays.reduceRight((a, v) => v.flatMap(c => a.map(r => c r)));
console.log(combine(arrays));
1 For your case that would be as simple as const arrays = [arr1, ...arr2];
CodePudding user response:
var arr1 = ['A','B','C'];
var arr2 = [['D','E','F'],['G','H','I'],['J','K','L'],['M','N','O']];
const arr3 = [];
arr1.forEach((item,index)=>{
if(arr2.length >= index)
{
arr2.forEach((el,i)=>{
el.forEach((char,charIndex)=>{
let combination = item
combination = char
for(let j=1;j<arr2.length;j ){
if(arr2[j]){
combination = arr2[j][charIndex]
}
}
arr3.push(combination)
})
})
}
})
console.log(arr3)
here is what I did