Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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