多組信息,不同的組合,一共可以有哪些組合呢, 這里給大家一個JS數(shù)組組合實例分享
var values = [ ["小明","小紅"], ["語文","數(shù)學","英語"], ]; //笛卡爾乘積 組合原理 將二維數(shù)組進行多重組合 function arrToCombination(arr) { var next = arr.pop();//刪除數(shù)組中的最后一個元素, 返回刪除的元素 while(arr.length > 0) { var t = []; var pre = arr.pop(); //前一個數(shù)組 if(! Array.isArray(pre)) pre = [pre]; for(x in pre) { for(y in next){ var v = []; v.push(pre[x]); v = v.concat( Array.isArray(next[y]) ? next[y] : [next[y]]); t.push(v); } } next = t; } return next; } console.log(arrToCombination(values));
