For example I have an array like this
var arr = [
['Red', 'Black'],
['S', 'M'],
['Cotton', 'Linen']
]
How to have the result with an array likes:
result: [
'Red - S - Cotton',
'Red - M - Cotton',
'Red - S - Linen',
'Red - M - Linen',
'Black - S - Cotton',
...
]
This is the code I coded before, but it not work as I want :( the result is not good as I want https://jsfiddle.net/minhthe208/shkz7av3/47/ (please see the console, it's duplicated name) and miss some names, for example: Black - M - Cotton is missed
var arr = [
['Red', 'Black'],
['S', 'M'],
['Cotton', 'Linen']
]
var sum = 1;
for (var i = 0; i < arr.length; i++) {
sum *= arr[i].length;
}
function FullRow(arr, len) {
var index = 0
var result = []
var current = 0
while (index < len) {
if (current === arr.length)
current = 0
result[index] = arr[current]
index++
current++
}
return result
}
function Combine(arr, len) {
var result = arr[0];
for (var index = 1; index < arr.length; index++) {
for (var current = 0; current < len; current++) {
result[current] += ' - ' + arr[index][current];
}
}
return result;
}
var combineArr = [];
for (var i = 0; i < arr.length; i++) {
combineArr.push(FullRow(arr[i], sum));
}
console.log('----COMBINE----')
var finalArr = Combine(combineArr, sum)
console.log(finalArr)
What should I do to have the result as above?