js編寫一個數組笛卡爾積算法
阿新 • • 發佈:2018-04-28
con str console class span [] 個數 nts 一個數
function getProducts(specs) { if (!specs || specs.length == 0) { return []; } else { return joinSpec([[]], specs, 0, specs.length-1); } function joinSpec(prevProducts, specs, i, max) { var currentProducts = [], currentProduct, currentSpecs = specs[i]; if( i > max ) { return prevProducts; } prevProducts.forEach(function(prevProduct) { currentSpecs.forEach(function(spec) { currentProduct = prevProduct.slice(0); currentProduct.push(spec); currentProducts.push(currentProduct); }); });return joinSpec(currentProducts, specs, ++i, max); } }
var specs = [[{"spec_name":"顏色","spec_type":"1","spec_id":"5","spec_value_id":"11","spec_value_name":"白色"},{"spec_name":"顏色","spec_type":"1","spec_id":"5","spec_value_id":"12","spec_value_name":"黑色"}],[{"spec_name":"尺碼","spec_type":"0","spec_id":"6","spec_value_id":"9","spec_value_name":"10"},{"spec_name":"尺碼","spec_type":"0","spec_id":"6","spec_value_id":"10","spec_value_name":"20"}],[{"spec_name":"材質","spec_type":"0","spec_id":"7","spec_value_id":"13","spec_value_name":"鉆石"},{"spec_name":"材質","spec_type":"0","spec_id":"7","spec_value_id":"14","spec_value_name":"水晶"},{"spec_name":"材質","spec_type":"0","spec_id":"7","spec_value_id":"15","spec_value_name":"瑪瑙"}]];
console.log(getProducts(specs));
js編寫一個數組笛卡爾積算法