I have the following objects:
var category = [
{"category" : "fruity"},
{"category" : "Cakes"}
]
and
var products = [
{"description" : "Apple", "price" : 12.99, "category" : "Fruity"},
{"description" : "Peach", "price" : 12.99, "category" : "Fruity"},
{"description" : "Cake one", "price" : 12.99, "category" : "Cake"}
]
I want to make this information available in the following format:
[
{
"category": "fruity",
"products": [
{
"description" : "Apple",
"price" : 12.99
},
{
"description" : "Peach",
"price" : 17.99
}
]
},
{
"category": "Cakes",
"products": [
{
"description" : "Cake one",
"price" : 12.99
}
]
}
]
I'm working on this at one time and the result has not been satisfactory. Here is my code:
var j=0;
var i=0;
var entraP = { prod : [] }
if (category[i].category == products[j].category ){
entrarP.c = category[i].category;
while(category[i].category == products[j].category ){
entrarP.prod.push(products[j]);
j+=1;
}
i+=1;
}
console.log(entrarP);
Thank you for your help.