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.
Cartesian product combination function Descartes (list) {//parent upper index; Count pointer count var point = {};
var result = [];
var pindex = null;
var tempcount = 0;
var temp = []; Generates a pointer object for a parameter column for (var index in list) {if (typeof list[index] = = ' object ') {Point[index] = {' Parent ':p in
Dex, ' Count ': 0} pindex = index;
}//A single dimension data structure directly returns if (Pindex = = null) {return list;
///dynamic generation of Cartesian product while (true) {for (var index in list) {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 }
}
}
}
The
wants this article to help you with your JavaScript programming.