这一段时间在搞商品后台的系统,其中关于通过商品的销售属性,生成SKU的处理,这里说明一下。
/*** 生成笛卡尔积* @returns {*}*/
function descartes(array){if( array.length < 2 ) return array[0] || [];return [].reduce.call(array, function(col, set) {var res = [];col.forEach(function(c) {set.forEach(function(s) {var t = [].concat( Array.isArray(c) ? c : [c] );t.push(s);res.push(t);})});return res;});
}
原有数组
[ ['黑色', '白色', '蓝色'], ['1.2KG', '2.0KG', '3.0KG'] ]
执行以后数组
[['黑色', '1.2KG'],['黑色', '2.0KG'],['黑色', '3.0KG'],['白色', '1.2KG'],['白色', '2.0KG'],['白色', '3.0KG'],['蓝色', '1.2KG'],['蓝色', '2.0KG'],['蓝色', '3.0KG'],
]