The implementation method of JavaScript Cartesian product algorithm

Source: Internet
Author: User

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 } } } }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.