抽象問題:你面前有n個筐,每一個筐都放着不一樣數量的不一樣物品,讓你在每一個筐中拿一個物品,請問一共有多少可能,分別是什麼?數組
function combine(twoDimensionalArr) { //二維數組中每一個數組的最大長度 var maxLengthArr = []; //遍歷過程當中每一個數組的下標 var tempIndexArr = []; for (var i = 0; i < twoDimensionalArr.length; i++) { maxLengthArr.push(twoDimensionalArr[i].length); tempIndexArr.push(0); } //組合一共有多少種 var count = 1; for (var i = 0; i < maxLengthArr.length; i++) { count = count * maxLengthArr[i]; } var combineValues = []; var combineItem; for (var i = 0; i < count; i++) { if (i !== 0) { var changeIndex = tempIndexArr.length - 1; var sholdAdd = false; while (!sholdAdd) { tempIndexArr[changeIndex] += 1; if (tempIndexArr[changeIndex] >= maxLengthArr[changeIndex]) { tempIndexArr[changeIndex--] = 0; sholdAdd = false; } else { sholdAdd = true } } } combineItem = []; for (var j = 0; j < tempIndexArr.length; j++) { combineItem.push(twoDimensionalArr[j][tempIndexArr[j]]); } combineValues.push(combineItem); } console.log(combineValues); return combineValues; } combine([[1,2,3],["a","b","c","d"],["A","B"]]);`