在創軟小(xiǎo)程序開發電(diàn)商(shāng)項目時,遇到商(shāng)品sku組合算法,本以為(wèi)sku挺簡單,但還是有(yǒu)一點難度。創軟小(xiǎo)程序開發團隊将電(diàn)商(shāng)項目開發中(zhōng)的sku組合算法js版本整理(lǐ)如下,供參考學(xué)習。
<html> <head> <script type="text/javascript"> function f_sku_chk(list, count, array, ind, start, indexs) { start++; if (start > count - 1) { return; } if (start == 0) { indexs = new Array(array.length); } for (indexs[start] = 0; indexs[start] < array[start].length; indexs[start]++) { f_sku_chk(list, count, array, 0, start, indexs); if (start == count - 1) { var temp = new Array(count); for (var i = count - 1; i >= 0; i--) { temp[start - i] = array[start - i][indexs[start - i]]; } list.push(temp); } } } var temp = [ ["A", "B", "C"], ["1", "2"], ["X1", "X2", "X3"] ]; var list = []; f_sku_chk(list, temp.length, temp, 0, -1); for (var i = 0; i < list.length; i++) { console.log(list[i]); } </script> </head> <body> 完成,請查看控制台輸出 </body> </html>