This article mainly introduces the implementation method of the javascript Cartesian product algorithm. The example analyzes the javascript implementation skills of the Cartesian product algorithm, which has some reference value, for more information about how to implement the javascript Cartesian product algorithm, see the example in this article. Share it with you for your reference. The specific analysis is as follows:
The Cartesian product can be generated based on the given object or array.
// Function descartes (list) {// upper-level index of parent; count pointer count var point ={}; var result = []; var pIndex = null; var tempCount = 0; var temp = []; // generates the pointer object for (var index in list) {if (typeof list [index] = 'object') based on the parameter column ') {point [index] = {'parent': pIndex, 'count': 0} pIndex = index;} // if (pIndex = null) is returned for a single-dimension data structure) {return list;} // dynamically generate the Cartesian product while (true) {for (var index in list) {tempCount = point [index] ['count']; temp. push (list [index] [tempCount]);} // Press the result array result. push (temp); temp = []; // Check the maximum pointer value while (true) {if (point [index] ['count'] + 1> = list [index]. length) {point [index] ['count'] = 0; pIndex = point [index] ['parent']; if (pIndex = null) {return result ;} // assign a value to parent and check again index = pIndex;} else {point [index] ['count'] ++; break ;}}}}
I hope this article will help you design javascript programs.