js笛卡尔积

来源:互联网 发布:单项软件开发资质 编辑:程序博客网 时间:2024/05/29 16:31

js笛卡尔积

可以用来模拟路径,或者标签的顺序。

//笛卡儿积组合function descartes(list){//parent上一级索引;count指针计数var point = {};var result = [];var pIndex = null;var tempCount = 0;var temp  = [];//根据参数列生成指针对象for(var index in list){if(typeof list[index] == 'object'){point[index] = {'parent':pIndex,'count':0}pIndex = index;}}//单维度数据结构直接返回if(pIndex == null){return list;}//动态生成笛卡尔积while(true){for(var index in list){tempCount = point[index]['count'];temp.push(list[index][tempCount]);}//压入结果数组result.push(temp);temp = [];//检查指针最大值问题while(true){if(point[index]['count']+1 >= list[index].length){point[index]['count'] = 0;pIndex = point[index]['parent'];if(pIndex == null){return result;}//赋值parent进行再次检查index = pIndex;}else{point[index]['count']++;break;}}}}//var ss=descartes([[1,2,3],[1,2]]);<<--多重数组//var ss=descartes({"a":[1,2,3],"b":[1,2]});<<--json //console.log(ss);
原创粉丝点击