Skip to content

Instantly share code, notes, and snippets.

@wayspurrchen
Last active February 20, 2016 19:08
Show Gist options
  • Save wayspurrchen/3b86cff84cdafca6e9e6 to your computer and use it in GitHub Desktop.
Save wayspurrchen/3b86cff84cdafca6e9e6 to your computer and use it in GitHub Desktop.
var categories = [
{
id: 0,
name: 'Shoes',
imageUrl: 'alkfaekfja0-43i'
},
{
id: 1,
name: 'Clothing',
imageUrl: 'someshit'
},
{
id: 2,
name: 'Facial Care',
imageUrl: ''
}
];
var products = [
{
name: 'Fancy Shoes',
categories: [ 0, 1 ]
imageUrl: ''
},
{
name: 'Some Lipstick',
categories: [ 2 ],
imageUrl: ''
}
];
function getCategory ( categoryName ) {
for ( var i = 0; i < categories.length; i++ ) {
if ( categories[ i ].categoryName === categoryName ) {
return categories[ i ];
}
}
}
function getProductsByCategory ( categoryName ) {
var category = getCategory( categoryName );
var productsIWant = [];
for ( var i = 0; i < products.length; i++ ) {
for ( var j = 0; j < products[ i ].categories.length; j++ ) {
if ( products[ i ].categories[ j ] === category.id ) {
productsIWant.push( products );
}
}
}
return productsIWant;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment