jQuery / Javascript多数组合 [英] jQuery/Javascript Multiple Array Combinations

查看:102
本文介绍了jQuery / Javascript多数组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图找到解决方案但没有用。我想要实现的目标是能够找到多个列表的所有独特组合。所以,假设我有3个复选框列表(但这是现实应用程序中的未知数字),颜色,大小,包大小。列表中的项目将是unqiue。

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 combinations 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']

我想得到蓝色,小号,6件装蓝色,中等,6件装蓝色,大号,6件装蓝色,小号,8件装蓝色,中等,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.

我已经使用jQuery将列表拉入数组:

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.

干杯 - 丹

推荐答案

这应该有效:)

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 ( );

您的数组

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

结果将是这样的:


  1. 蓝色小包6

  2. 蓝色小包装8

  3. 蓝色中等包装6

  4. ...

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

这篇关于jQuery / Javascript多数组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆