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 :)

2
  • 2
    I really don't understand question! Why its' ['two','four']? What's the catch i mean based on what you need products' category? Commented Aug 21, 2015 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; }); Commented Aug 21, 2015 at 11:19

4 Answers 4

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;
  });
});
Sign up to request clarification or add additional context in comments.

4 Comments

This uses a library which hasn't been mentioned by the poster
At the very least mention which library this code uses.
Yeah, should have mentioned I use lodash indeed.. But maybe there was a cleaner "plain" JavaScript solution, like that more :)
Thanks, this worked! (although you do have a typo "categrories" ;-) ) thanks again!
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);

Comments

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;
        } 
    }
}

Comments

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

Comments

Your Answer

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