Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function normalizeDependOn(key, id) {
var depend = id || "id";
return key.reduce(function(data,oldData) {
data[oldData[depend]] = oldData;
return data;
},{});
}
var data = {
"food": [
{
categoryId:"123",
foods:["ghorme, gheimeh, fesenjoon"]
},
{
categoryId:"345",
foods:"gilas"
},
{
categoryId:"5456",
foods:"bademjoon"
}
],
"category": [
{
id:"123",
name:"khoshmazeha"
},
{
id:"345",
name:"nestbanha"
},
{
id:"5456",
name:"eeey"
}
]
};
var normalCategory = normalizeDependOn(data["category"]);
var normalFood = normalizeDependOn(data["food"],"categoryId");
console.log("category",normalCategory);
console.log("food",normalFood);
console.log("delicious foods",normalFood['123'].foods); // ["ghorme, gheimeh, fesenjoon"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.