let skuList = [ ['黑色', '白色','黄色'], ['64G', '128G', '512G'], ]
[ ["黑色", "64G"] ["黑色", "128G"] ["黑色", "512G"] ["白色", "64G"] ["白色", "128G"] ["白色", "512G"] ["黄色", "64G"] ["黄色", "128G"] ["黄色", "512G"] ]
function eachArr(arr1,arr2){ return arr1.map(function(item1){ return arr2.map(function(item2){ return item1 instanceof Array ? item1.concat(item2):[item1,item2]; }) }).reduce( function(accumulator, currentValue){ return accumulator.concat(currentValue) }) } [['黑色', '白色','黄色'], ['64G', '128G', '512G'],].reduce(function(accumulator, currentValue){ return eachArr(accumulator,currentValue) })
例如:
[ ['黑色', '白色','黄色'], ['64G', '128G', '512G'],['大', '中', '小'],.......].reduce(function(accumulator, currentValue){ return eachArr(accumulator,currentValue) }) 返回值是 ["黑色", "64G", "大"] ["黑色", "64G", "中"] ["黑色", "64G", "小"] ["黑色", "128G", "大"] ["黑色", "128G", "中"] ["黑色", "128G", "小"] ["黑色", "512G", "大"] ["黑色", "512G", "中"] ["黑色", "512G", "小"] ["白色", "64G", "大"] ["白色", "64G", "中"] ["白色", "64G", "小"] ["白色", "128G", "大"] ["白色", "128G", "中"] ["白色", "128G", "小"] ["白色", "512G", "大"] ["白色", "512G", "中"] ["白色", "512G", "小"] ["黄色", "64G", "大"] ["黄色", "64G", "中"] ["黄色", "64G", "小"] ["黄色", "128G", "大"] ["黄色", "128G", "中"] ["黄色", "128G", "小"] ["黄色", "512G", "大"] ["黄色", "512G", "中"] ["黄色", "512G", "小"]
##### 整体实现思路就是二维数组里的第一个数组与后一个数组组合。而后组合后的数组,再与后一个数组组合。依次累计组合就能够了。美滋滋。吃了一顿蜜汁鸡。数组
这是这是简单的实现一种方式,若是要加上其余业务或是功能能够再eachArr里面区改动。 如要帮忙改动的,能够留言哦。