I have a json file that's returned from a service am using. I have no control over the service therefore can't change the structure of the json file. The file looks something like:
menu":[
{
"section":"theMobileMenu",
"key":"menuItem1Title",
"content":"Mobile context and principles"
},
{
"section":"theMobileMenu",
"key":"menuItem1Link",
"content":"/mobile/index.html"
},
{
"section":"theMobileMenu",
"key":"menuItem2Title",
"content":"Global guidelines"
},
{
"section":"theMobileMenu",
"key":"menuItem2Link",
"content":"/mobile/global-guidelines.html"
},
{
"section":"theMobileMenu",
"key":"menuItem3Title",
"content":"First impressions"
},
{
"section":"theMobileMenu",
"key":"menuItem3Link",
"content":"/mobile/first-impressions.html"
}
]
I want to create an array of objects like the one below
"menu":[
{
"title":"Mobile context and principles",
"link":"/mobile/index.html"
},
{
"title":"Global guidelines",
"link":"/mobile/global-guidelines.html"
},
{
"title":"First impressions",
"link":"/mobile/first-impressions.html"
}
]
I've tried something like:
var newData = []
var curData = {};
var x = 1;
$.each(data.menu, function(i, val) {
if(val.key == 'menuItem'+x+'Link'){
curData.link = val.content;
}
if(val.key == 'menuItem'+x+'Title'){
curData.title = val.content;
}
newData.push(curData)
curData = []
x++;
})
This doesn't work very well. Any ideas on how to work this out?
datashould bedata.menu.