Home > Article > Web Front-end > Cartesian product algorithm and multiple array Cartesian product in JS (detailed tutorial)
This article mainly introduces the JS Cartesian product algorithm and the implementation method of multiple array Cartesian products. It analyzes the related operation skills of javascript to generate Cartesian products based on objects or arrays in the form of examples. Friends who need it can refer to it
The examples in this article describe the JS Cartesian product algorithm and the implementation method of multiple array Cartesian products. Share it with everyone for your reference. The details are as follows:
js Implementation code of Cartesian product algorithm, generates Cartesian product based on objects or arrays, and introduces a javascript example of multiple array Cartesian product, as well as java implementation Cartesian product algorithm and example code.
1. JavaScript Cartesian product algorithm code
Example, generate a Cartesian product based on an object or array.
//笛卡儿积组合 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; } } } }
Calling method:
var result = descartes({'aa':['a','b','c','d'],'bb':['$','%','^','&']}); alert(result);//result就是笛卡尔积
2. JS implements multiple array Cartesian product
Example:
<script> (function() { dwn = function(a) { document.writeln(a + "<br />") }; //笛卡尔积 var Cartesian = function(a, b) { var ret = []; for (var i = 0; i < a.length; i++) { for (var j = 0; j < b.length; j++) { ret.push(ft(a[i], b[j])); } } return ret; } var ft = function(a, b) { if (! (a instanceof Array)) a = [a]; var ret = Array.call(null, a); ret.push(b); return ret; } //多个一起做笛卡尔积 multiCartesian = function(data) { var len = data.length; if (len == 0) return []; else if (len == 1) return data[0]; else { var r = data[0]; for (var i = 1; i < len; i++) { r = Cartesian(r, data[i]); } return r; } } })(); var data = [['a', 'b', 'c'], [1, 2, 3, 4], ['A', 'B'], ['#', '@', '+'], ['Mary', 'Terry', 'KYO']]; var r = multiCartesian(data); for (var i = 0; i < r.length; i++) { dwn("(" + r[i] + ")"); } </script>
The above is what I compiled for everyone, I hope It will be helpful to everyone in the future.
Related articles:
Detailed introduction to the usage of ref ($refs) in Vue.js
Detailed interpretation of father-son communication in vue
How to implement the numeric keyboard component using Vue
using nginx nodeThe above is the detailed content of Cartesian product algorithm and multiple array Cartesian product in JS (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!