SoFunction
Updated on 2025-04-08

Implementation method of Cartesian Integration Algorithm of JavaScript

This article describes the implementation method of the Cartesian product algorithm of JavaScript. Share it for your reference. The specific analysis is as follows:

Here, Cartesian product can be generated based on the given object or array

//Cartesian product combinationfunction descartes(list)
{
  //parent previous level index; count pointer count  var point = {};
  var result = [];
  var pIndex = null;
  var tempCount = 0;
  var temp  = [];
  //Generate a reference object based on the parameter column  for(var index in list)
  {
    if(typeof list[index] == 'object')
    {
      point[index] = {'parent':pIndex,'count':0}
      pIndex = index;
    }
  }
  //Return directly to the single-dimensional data structure  if(pIndex == null)
  {
    return list;
  }
  //Dynamic generation of Cartesian products  while(true)
  {
    for(var index in list)
    {
      tempCount = point[index]['count'];
      (list[index][tempCount]);
    }
    //Press into the result array    (temp);
    temp = [];
    //Ask for checking 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 parent to check again        index = pIndex;
      }
      else
      {
        point[index]['count']++;
        break;
      }
    }
  }
}

I hope this article will be helpful to everyone's JavaScript programming.