将嵌套的JSON重组/合并为简单的JSON对象

时间:2019-07-16 23:05:14

标签: javascript json angular

我有一个JSON数组,其中的对象包含标题和主题标记数组。我想使其成为一个简单的json文件,并具有所有处于第一级的值。

这就是我所拥有的:

options.UseSqlite

到目前为止,我尝试了array.map和reduce函数,还尝试了lodash.results(lodash.find(data,“ title”)),然后将其与列名映射。它没有用。

[
   {
      "title":"mca",
      "result":[
         {
            "sub1":1,
            "sub2":2
         },
         {
            "sub1":1.23,
            "sub2":1.25
         },
         {
            "sub1":3.4,
            "sub2":2.3
         }
      ]
   },
   {
      "title":"bca",
      "result":[
         {
            "sub1":1.1,
            "sub2":2.8
         },
         {
            "sub1":1.15,
            "sub2":1.18
         },
         {
            "sub1":1.2,
            "sub2":1.9
         }
      ]
   }   
]

预期输出:

const arr = [
   {
      "title":"mca",
      "result":[
         {
            "sub1":1,
            "sub2":2
         },
         {
            "sub1":1.23,
            "sub2":1.25
         },
         {
            "sub1":3.4,
            "sub2":2.3
         }
      ]
   } 
];

const test = [].concat(...arr.map((item) => item.results.map((results) => Object.assign({}, item, { results }))));

console.log(test);

0 个答案:

没有答案