根据现有JSON数组的嵌套键/值创建一个新的JSON数组

时间:2018-08-12 23:31:01

标签: javascript arrays ecmascript-5

我有一个对象数组,它们的结构总是这样:

var initialJSON = [  
  {  
     "id":"01",
     "name":"Jane",
     "project":{  
        "title":"Alpha",
        "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
        "city":"New York"
     },
     "dateCreated":"2018-04-28 04:11:16 UTC"
   },
   {  
     "id":"02",
     "name":"Sarah",
     "project":{  
       "title":"Beta",
       "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
       "city":"London"
     },
      "dateCreated":"2018-04-28 04:11:29 UTC"
    },
    {  
      "id":"03",
      "name":"Mia",
      "project":{  
         "title":"Gamma",
         "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
         "city":"Paris"
      },
      "dateCreated":"2018-04-28 04:11:29 UTC"
   }
];

我的目标是创建一个仅包含每个项目项目的键/值的Objects新数组。本质上是从每个项目中删除所有其他数据(包括父键“项目”)。

最终结果如下:

var formattedJSON = [
  {
    "title":"Alpha",
    "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
    "city":"New York"
  },
  {
    "title":"Beta",
    "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
    "city":"London"
  },
  {
    "title":"Gamma",
    "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
    "Paris"
  }
]

我很难找到一种遍历数据并获得正确结果的方法。我知道已经发布了类似的问题,但似乎没有从内键的嵌套键/值中创建一个新数组。

3 个答案:

答案 0 :(得分:2)

为此,您可以使用Array#map(),方法是将数组的每个元素分配给其project属性:

let initialJSON=[{"id":"01","name":"Jane","project":{"title":"Alpha","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit","city":"New York"},"dateCreated":"2018-04-28 04:11:16 UTC"},{"id":"02","name":"Sarah","project":{"title":"Beta","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit","city":"London"},"dateCreated":"2018-04-28 04:11:29 UTC"},{"id":"03","name":"Mia","project":{"title":"Gamma","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit","city":"Paris"},"dateCreated":"2018-04-28 04:11:29 UTC"}];

let result = initialJSON.map(element => element.project);

console.log(result)

答案 1 :(得分:0)

您可以通过以下方式在map数组上使用initialJSON函数:

var formattedJSON = initialJSON.map(item => {

  // For each item in the "initialJSON", map the fields to each item 
  // in the "formattedJSON" array
  return {
    title : item.project.title,
    description : item.project.description,
    city : item.project.city
  }

})

答案 2 :(得分:0)

JSON.parse reviver parameter可用于更改或排除值:

var j = '[{"id":"01","name":"Jane","project":{"title":"Alpha","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit","city":"New York"},"dateCreated":"2018-04-28 04:11:16 UTC"},{"id":"02","name":"Sarah","project":{"title":"Beta","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit","city":"London"},"dateCreated":"2018-04-28 04:11:29 UTC"},{"id":"03","name":"Mia","project":{"title":"Gamma","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit","city":"Paris"},"dateCreated":"2018-04-28 04:11:29 UTC"}]'

let result = JSON.parse(j, (k, v) => v.project || v);

console.log( result )