0

I was wondering how I can check this. Example:

var products = [
    {
        title: 'Product 1',
        categories: ['One', 'Two', 'Three']
    },
    {
        title: 'Product 2',
        categories: ['Three', 'Four']
    }
];

var categories = ['Two','Four'];

How can I get the two products matching one of the categories? Hope someone can help me out :)

Dhaval Marthak
  • 17,246
  • 6
  • 46
  • 68
  • 2
    I really don't understand question! Why its' `['two','four']`? What's the catch i mean based on what you need products' category? – Dhaval Marthak Aug 21 '15 at 11:15
  • I want to filter the products matching the categories array (with lodash) _.find(products, function(item) { return item.categories.indexOf(categories) > -1; }); –  Aug 21 '15 at 11:19

4 Answers4

6

plain js:

products.filter(function(product) {
  return categories.some(function(cat) {
     return product.categories.indexOf(cat) >= 0;
  });
});

lodash:

_.filter(products, function(product) {
  return _.some(categories, function(cat) {
     return _.indexOf(product.categories, cat) >= 0;
  });
});
manji
  • 47,442
  • 5
  • 96
  • 103
  • 2
    This uses a library which hasn't been mentioned by the poster – jonny Aug 21 '15 at 11:23
  • 1
    At the very least mention *which* library this code uses. – JJJ Aug 21 '15 at 11:23
  • Yeah, should have mentioned I use lodash indeed.. But maybe there was a cleaner "plain" JavaScript solution, like that more :) –  Aug 21 '15 at 11:24
  • Thanks, this worked! (although you do have a typo "categrories" ;-) ) thanks again! –  Aug 21 '15 at 11:25
0

Using simple for:

var products = [
    {
        title: 'Product 1',
        categories: ['One', 'Two', 'Three']
    },
    {
        title: 'Product 2',
        categories: ['Three', 'Four']
    }
];

var categories = ['Two', 'Four'];

var list = []
for (x in products) {
    for (y in products[x].categories) {
        if (categories.indexOf(products[x].categories[y]) != -1) {
            list.push(products[x].title)
        }
    }
}

console.log(list) //here your match
Regent
  • 5,142
  • 3
  • 21
  • 35
Shailendra Sharma
  • 6,976
  • 2
  • 28
  • 48
0

Not very good solution from code side but it can be clear (if someone don't understand) and it's works

var products = [
    {
        title: 'Product 1',
        categories: ['One', 'Two', 'Three']
    },
    {
        title: 'Product 2',
        categories: ['Three', 'Four']
    }
];

var categories = ['Two','Four'];
var retArr = [];


for (i = 0; i < categories.length; ++i) {
  for (j = 0; j < products.length; ++j) {
     if(products[j].categories.indexOf(categories[i]) > -1){ // check if product categories contains some category. If yes, then add to array
            retArr.push(products[j]);
        }
    }
}

console.log(retArr);
demo
  • 6,038
  • 19
  • 75
  • 149
0

Just simple looping through:

var foundproducts = [];
for (i = 0; i < products.length; i++) {
    for (u = 0; u < categories.length; u++) { 
        if (products[i].categories.indexOf(categories[u]) != -1) { 
            foundproducts.push(products[i].title);
            break;
        } 
    }
}
Regent
  • 5,142
  • 3
  • 21
  • 35
OSDM
  • 265
  • 1
  • 2
  • 10