So writing an application which uses a lot of user generated data. And in most cases I just use the mongodb id to look up whatever it is. So a typical example for this app is icons for categories. They are stored as a data set that gets passed around the application from the api that speaks to the mongodb to the api to a factory and then to the controller, and quite often I need to make a simple lookup. Lets say that the category looks as follows:
[
{
"_id":"55dc79efed0fcf4a58d4a68d",
"categoryName":"Sport",
"categoryDescription":"Sport and more sports",
"categoryIcon":"55dc79b2ed0fcf4a58d4a68c",
"__v":0
}
]
categoryIcon is a reference to another table, at the time loaded in a factory, data looks as follows.
[
{
"_id":"55dc79b2ed0fcf4a58d4a68c",
"iconName":"Sport",
"cssString":"fa fa-futbol-o",
"__v":0
},
{
"_id":"55dc79b2ed0fcf4a58d4a69d",
"iconName":"Travel",
"cssString":
"fa fa-plane",
"__v":0
}
]
So the way I have been resolving this is along the lines of
for (var i in icons){
if( icons[i]._id == category.categoryIcon ){
console.log("Found the Icon");
category.cssString=icons[i].cssString;
}
}
Easy, relatively fast, but seems a bit wasteful, so is there a smoother way. Made a simple jfiddle if anyone wants to test.
Aucun commentaire:
Enregistrer un commentaire