在創(chuàng)軟小程序開發(fā)電商項(xiàng)目時(shí),遇到商品sku組合算法,本以為sku挺簡(jiǎn)單,但還是有一點(diǎn)難度。創(chuàng)軟小程序開發(fā)團(tuán)隊(duì)將電商項(xiàng)目開發(fā)中的sku組合算法js版本整理如下,供參考學(xué)習(xí)。
<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> 完成,請(qǐng)查看控制臺(tái)輸出 </body> </html>