In this paper, we illustrate the implementation of JavaScript Cartesian product algorithm. Share to everyone for your reference. The specific analysis is as follows:
This can be used to generate Cartesian product based on the given object or array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the same as the |
//Cartesian product combination function Descartes (list) {//parent upper level index; Count pointer count var point = {}; var result = []; var pindex = null; var temp Count = 0; var temp = []; To generate a pointer object for (VAR index in list) {if (typeof list[index] = = ' object ') {Point[index] = {' Parent ':p index, ' count ': 0} pIn Dex = index; }//A single dimension data structure directly returns if (Pindex = = null) {return list;}///dynamically generated Cartesian product while (true) {for (var index) {Tempcount = point[ index][' count ']; Temp.push (List[index][tempcount]); //press into the result array result.push (temp); temp = []; Check the pointer maximum problem while (true) {if (point[index][' count ']+1 >= list[index].length) {point[index][' count '] = 0; pindex = point [Index] [' Parent ']; if (Pindex = = null) {return result;}///assign parent to check again index = Pindex; else {point[index]['Count ']++; Break } } } } |