我有一个json文件,它是从使用的服务返回的。我无法控制服务,因此无法更改json文件的结构。该文件类似于:
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"
}
]
我想创建一个像下面那样的对象数组
"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"
}
]
我尝试过类似的事情:
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++;
})
这种方法效果不佳。关于如何解决这个问题的任何想法?
答案 0 :(得分:1)
试试这个:
var data = [{
"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"
}];
var obj = {};
var menu = data.map(function(v, k) {
if (v.key.match(/menuItem\d*Title/) !== null) {
obj = {
title: v.content
};
} else if (v.key.match(/menuItem\d*Link/) !== null) {
obj.link = v.content;
return obj;
}
});
menu = $.grep(menu, function(n) {
return n == 0 || n
});
只有当密钥按照OP中所示的确切顺序执行时,代码才有效。
答案 1 :(得分:0)
使用for
循环尝试使用以下代码更容易..如果您的数据符合上述顺序
var data = {
"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"
}
]
};
var newData = [];
var curData = {};
var menu = data.menu;
for(i=0, j=1; i < menu.length; i+=2, j++){
if(menu[i]['key'] == "menuItem"+j+"Title"){
curData.title = menu[i]['content'];
if($.isPlainObject(menu[i+1]) && menu[i+1]['key'] == "menuItem"+j+"Link"){
curData.link = menu[i+1]['content'];
}
newData.push(curData);
}
curData = {};
}
console.log(newData);
输出:
[
{
"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"
}
]
答案 2 :(得分:0)
此解决方案适用于任何带有临时对象和一些正则表达式的订单,用于匹配键,标题和链接。
var object = { 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" }] },
result = function (array) {
var temp = {}
array.forEach(function (a) {
var k = a.key.match(/^menuItem\d*/);
temp[k] = temp[k] || {};
temp[k][a.key.match(/(Title)|(Link)$/)[0].toLowerCase()] = a.content;
});
return Object.keys(temp).map(function (k) { return temp[k]; });
}(object.menu);
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
&#13;
答案 3 :(得分:0)
<强> https://jsfiddle.net/VixedS/01x2a8jp/ 强>
var oldData = {menu:[{ "section": "theMobileMenus", "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"}]};
var newData = []
$.each(oldData.menu, function(i, val){
if (this.key.indexOf('Link') > -1)
newData.push({'title':this.section,'link':this.content});
});
newData=JSON.stringify(newData);