JS Cartesian product
Can be used to simulate the path, or the order of the labels.
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 index, ' count ': 0}
pindex = index;
}
}
The single dimensional data structure directly returns
if (Pindex = = null)
{return
list;
}
Dynamically generating 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
;
}}} var ss=descartes ([[[1,2,3],[1,2]]); <<--multiple array
//var ss=descartes ({"A": [1,2,3], "B": [1,2]}); <<-- JSON
//console.log (ss);