我的数据与json嵌套对象一样,但希望在任何javascript函数的帮助下将其格式化为简单的json数组格式。我尝试通过for循环来做但却失败了。
var dataModel: [ {
"phaseName" : "Closure",
"milestone" : [
{
"milestoneName" : "Mile-1",
"startDate" : ISODate("2017-08-06T18:30:00.000Z"),
"endDate" : ISODate("2017-07-06T18:30:00.000Z"),
"_id" : ObjectId("595a38e1ceb1973980406b7f")
},
{
"milestoneName" : "Mile-2",
"startDate" : ISODate("2017-07-04T18:30:00.000Z"),
"endDate" : ISODate("2017-07-13T18:30:00.000Z"),
"_id" : ObjectId("595a4394c7a5bb3120126721")
}
]
}
]
但我希望我的结果应该通过应用任何将dataModel转换为
的函数来形成newModel = [{
"phaseName":"Closure",
"MilestoneName":"Mile-1",
"startDate":"07/07/2017",
"endDate":"07/15/2017"
},
{
"phaseName":"Closure",
"MilestoneName":"Mile-2",
"startDate":"07/07/2017",
"endDate":"07/15/2017"
}
]
答案 0 :(得分:2)
如果仅用于调试,您可以使用console.table
答案 1 :(得分:0)
var newModel = [];
for(var i = 0; i < dataModel.length; i++){
for(var e = 0; e < dataModel[i].milestone.length;e++){
newModel.push({
"phase": dataModel[i].phaseName,
"milestone":dataModel[i].milestone[e].milestoneName,
"startDate":dataModel[i].milestone[e].startDate,
"endDate":dataModel[i].milestone[e].endDate,
})
console.log(newModel)