简体   繁体   English

从现有对象创建新的数组对象

[英]Create new array object from existing object

I have a json file that's returned from a service am using. 我有一个正在使用的服务返回的json文件。 I have no control over the service therefore can't change the structure of the json file. 我无法控制该服务,因此无法更改json文件的结构。 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? 关于如何解决这个问题的任何想法?

Try this: 尝试这个:

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
});

The code only works if the keys come in the exact order as shown in the OP. 该代码仅在按键按照OP中所示的确切顺序排列时才起作用。

Try below code using for loop makes easier.. If your data is in the sequence as mentioned above 尝试下面的代码,使用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);

Output : 输出:

[
   {
     "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"
  }
]

This solution works for any order with a temporary object and some regular expressions for matching key, title and link. 该解决方案适用于带有临时对象和一些用于匹配键,标题和链接的正则表达式的任何顺序。

 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>'); 

https://jsfiddle.net/VixedS/01x2a8jp/ 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM