0

I have a Multi-Dimension Array filled with products. I'm trying to filter my products by passing a bunch of values and then put the resulting products into a new array. Here's what i'm trying to accomplish:

products = [['A','2','F','123'],['A','2','G','234'],['B','2','K','231']];
related = [];

filter1 = 'A';
filter2 = '2';
filter3 = 'G';

for(var i = 0; i < products.length; i++) {
  var product = products[i];
  for(var j = 0; j < product.length; j++) {
    if(filter1=product[0]){
      related.push([product[0],product[1]....]);
    }
  }
}

Then from there, filter the resulting set with filter2 and so on and so forth. Can't seem to figure this out. Any help is greatly appreciated!

2 Answers 2

1

There's a few things going on here...

First, it looks like you want the Array.prototype.push along with the apply functions which can be used together to append the contents of an array onto another array (rather than appending the array as a single unit).

Array.prototype.push.appy(related, product);

Additionally, you want to use === for checking equality (the single = is for assignment only).

for(var i = 1; i < products.length; i++) {
  var product = products[i];
  if(filter1 === product[0] && filter2 === product[1] && filter3 === product[2]){
    Array.prototype.push.appy(related, product);
  } 
}
Sign up to request clarification or add additional context in comments.

3 Comments

fixed my post, i actually had it correct in my original code, but same thing applies, how do i filter on the related array with filter1/2 slowly making the array smaller?
@Damien - so you want to add the product array to related if all 3 filters are successful...?
Yea... that's awesome. Worked beautifully. I also didn't know that you could just add the array by doing related.push(product). I was going like: related.push([product[0],product[1],product[2]]). Definitely makes for cleaner code. Thanks a lot!
0

There are some suggestions I would add as follows:

1) Your outmost loop starts from i=1 instead of i=0, so the first element won't get filtered.

2) When you want a set of multiple filters to be applied, you should consider using an array for more convenience.

3) To add a new element to the array, use push, not the = assignment.

So let's try this:

products = [['A','2','F','123'],['A','2','G','234'],['B','2','K','231']];
related = [];

filters = ['A','2','G'];

for (var product of products) {
    var matchAllFilter = true;
    for (var f of filters){
        if (product.indexOf(f)<0){
            matchAllFilter = false;
            break;
        }
    }    
    if (matchAllFilter){
        related.push(product);
    }
}

4 Comments

fixed my post, i actually had it correct in my original code, but same thing applies, how do i filter on the related array with filter1/2 slowly making the array smaller?
Wait, do you want all filters to be applied as and operation like filter A,2,G means looking for an element which has all A,2,G instead of A or 2 or G?
Actually, yea... i guess the and would work, except it's filter1 == product[0], filter2 == product[1], filter3 == product[2]
OK, I have modified my code accordingly. This way you can vary your filter, one single filter, two, three, etc could work. Only product that match all filter will be taken into account.

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.