1

I am using jQuery isotope plugin to sort and filter elements. Elements are products having multiple filter groups. I am facing one issue, I need to filter products with multi select filter attribute in same group example:

Filter Groups: Gender, Product category, Product material.

Sample select : Men, Bag, Leather.

Above sample works fine.

But I need something like

New filter selection: Men, Bag, Wallet, leather, Rexin.

If you notice Bag and Wallet are from same group. And leather and Rexin are from same group.

for doing this I would need filter value to be passed like

filtervalue - .men.bag.leather,.men.wallet.leather,.men.bag.rexin,.men.wallet.rexin.

This sounds easy, but I am finding difficult to code this.

I have array of each filter group, which I am updating when the user selects or deselects a particular filter

So essentially, what I need is combining or grouping multiple filter arrays into one string with each group containing every element from each array and each group separated with a comma.

Arrays in javascript:

var gender = ['.men'];
var type = ['.bag','.wallet'];
var material = ['.leather','.rexin'];
Output:
".men.bag.leather,.men.wallet.leather,.men.bag.rexin,.men.wallet.rexin"

Hope I am clear on what I need.

Help much appreciated.

Update:

Since the filters can vary on different products, I am unable to use hardcoded variables like gender, type etc. So instead I am using filters array

var selectedFilter = [];
selectedFilter['gender'] = ['.men'];
selectedFilter['type'] = ['.bag','.wallet'];
selectedFilter['platform'] = ['.leather','.rexin'];

Need Output like, ".men.bag.leather,.men.wallet.leather,.men.bag.rexin,.men.wallet.rexin"

Need some function which I can directly pass one array variable. like

combine(selectedFilter);
0

1 Answer 1

2

You could use combination algorithm.

function combine(object) {
    function c(part, index) {
        array[index].forEach(function (a) {
            var p = part.concat([a]);
            if (p.length === array.length) {
                r.push(p.join(''));
                return;
            }
            c(p, index + 1);
        });
    }

    var array = Object.keys(object).map(function (k) { return object[k]; }),
        r = [];

    c([], 0);
    return r.join();
}

var selectedFilter = {                         // use an object instead of 
        gender: ['.men'],                      // an array with properties
        type: ['.bag','.wallet'],
        platform: ['.leather','.rexin']
    };

console.log(combine(selectedFilter));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.