最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jQueryJavascript Multiple Array Combinations - Stack Overflow

programmeradmin4浏览0评论

I have been trying to find a solution to this with no avail. The idea of what i'm trying to achieve is to be able to find all unique binations of multiple lists. So, let's say I have 3 lists of checkboxes (but this is an unknown number in the real-life application), Colour, Size, Pack Size. The items in the list's will be unqiue.

[0] => ['blue', 'green']
[1] => ['small', 'medium', 'large']
[2] => ['Pack Of 6', 'Pack Of 8']

I will want to get "Blue, Small, Pack Of 6", "Blue, Medium, Pack Of 6", "Blue, Large, Pack Of 6", "Blue, Small, Pack Of 8", "Blue, Medium, Pack Of 8" etc.. The ordering isn't important, but would be nice to have it logically grouped.

I already have the lists pulled out into an array using jQuery:

       options = [];

       jQuery('.option-types').each(function(){
            opts = [];
            jQuery('input:checked', this).each(function(){
                opts.push(jQuery(this).next().text());
            });
            options.push(opts)
        });

If there is a recursive functional path to answer this that would be ideal, as like i said the number of lists could be anything, aswell as the contents of the lists.

Hope you guys and girls can help, this is doing my head in.

Cheers - Dan

I have been trying to find a solution to this with no avail. The idea of what i'm trying to achieve is to be able to find all unique binations of multiple lists. So, let's say I have 3 lists of checkboxes (but this is an unknown number in the real-life application), Colour, Size, Pack Size. The items in the list's will be unqiue.

[0] => ['blue', 'green']
[1] => ['small', 'medium', 'large']
[2] => ['Pack Of 6', 'Pack Of 8']

I will want to get "Blue, Small, Pack Of 6", "Blue, Medium, Pack Of 6", "Blue, Large, Pack Of 6", "Blue, Small, Pack Of 8", "Blue, Medium, Pack Of 8" etc.. The ordering isn't important, but would be nice to have it logically grouped.

I already have the lists pulled out into an array using jQuery:

       options = [];

       jQuery('.option-types').each(function(){
            opts = [];
            jQuery('input:checked', this).each(function(){
                opts.push(jQuery(this).next().text());
            });
            options.push(opts)
        });

If there is a recursive functional path to answer this that would be ideal, as like i said the number of lists could be anything, aswell as the contents of the lists.

Hope you guys and girls can help, this is doing my head in.

Cheers - Dan

Share Improve this question edited Oct 28, 2009 at 10:29 dan richardson asked Oct 28, 2009 at 10:22 dan richardsondan richardson 3,9494 gold badges32 silver badges38 bronze badges 5
  • If i can get the output to be an array for each one that would be great - so [0] = ["Small", "Blue", "Pack Of 6"], [1] = ["Small, "Green", "Pack Of 8"], etc.. – dan richardson Commented Oct 28, 2009 at 10:25
  • Sry I don't get your question - I see what you have so far but what do you want? – jantimon Commented Oct 28, 2009 at 10:28
  • I mentioned above and below the first code block, to get the unqiue binations of all the items – dan richardson Commented Oct 28, 2009 at 10:31
  • I have found the following link which might do the job, but it's in python. If anyone can help just translate it that would be great! Thanks - code.activestate./recipes/496807 – dan richardson Commented Oct 28, 2009 at 10:46
  • See also stackoverflow./q/9422386/405017 for a 'lazy' (deferred) JavaScript version. – Phrogz Commented Feb 23, 2012 at 23:26
Add a ment  | 

2 Answers 2

Reset to default 7

This should work :)

var recursiveSearch;
var possibilities = [];

recursiveSearch = function (text, depth )
{
 text = text || "";
 depth = depth || 0;
 for ( var i = 0; i < options[depth].length; i++ )
 {
   // is there one more layer?
   if ( depth +1 < options.length )
     // yes: iterate the layer
     recursiveSearch ( text + ((text=="") ? "" : " ") + options[depth][i] , depth +1 );
   else
     // no: this is the last layer. we add the result to the array
     possibilities.push ( text + options[depth][i] );
 }
}


recursiveSearch ( );

with your array

[0] => ['blue', 'green']
[1] => ['small', 'medium', 'large']
[2] => ['Pack Of 6', 'Pack Of 8']

the result would be sth like this:

  1. blue small pack of 6
  2. blue small pack of 8
  3. blue medium pack of 6
  4. ...

You could modify the Java source code from page http://www.1your./drupal/FullCodeFindingAllStringCombinationsUsingRecursion to JavaScript - the idea should be easy to understand, even if you have not much exposure to Java. I doubt there is a ready-made solution to this.

发布评论

评论列表(0)

  1. 暂无评论