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"]
["白色", "64G"]
["白色", "128G"]
["白色", "512G"]
["黄色", "64G"]
["黄色", "128G"]
["黄色", "512G"]
]
例如:
[ ['黑色', '白色','黄色'], ['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里面区改动。
如要帮忙改动的,可以留言哦。
原文:https://www.cnblogs.com/koujinshidui/p/11959973.html