使用for循环的Node JS数据格式化

时间:2018-06-28 05:13:43

标签: javascript node.js

我有一个带有数据的对象数组:

[
                {
                    "id": 1,
                    "milestone": "Generated",
                    "phase_id": 1,
                    "phase": "ICP Assessment",
                    "activity_id": 1,
                    "activity": "Data Analysis",
                    "start_date": "2018-06-12T18:30:00.000Z",
                    "timeline": "3 weeks",
                    "responsibility": "2",
                    "status": "Not Started"
                },
                {
                    "id": 1,
                    "milestone": "Generated",
                    "phase_id": 1,
                    "phase": "ICP Assessment",
                    "activity_id": 2,
                    "activity": "Territory re-alignment",
                    "start_date": "2018-06-27T18:30:00.000Z",
                    "timeline": "2 Weeks",
                    "responsibility": "2",
                    "status": "Not Started"
                },
                {
                    "id": 2,
                    "milestone": "Identified",
                    "phase_id": 2,
                    "phase": "Pilot Planning",
                    "activity_id": 3,
                    "activity": "Existing ICP Discussion",
                    "start_date": "2018-06-27T18:30:00.000Z",
                    "timeline": "2 Weeks",
                    "responsibility": "2",
                    "status": "Not Started"
                }
            ]

我想将此数据转换为类似的内容

[
    {
        "id": 1,
        "milestone": "Generated",
        "phase":[
            {
                "phase_id": 1,
                "phase": "ICP Assessment",
                "activity"[
                    {
                        "activity_id": 1,
                        "activity": "Data Analysis",
                        "start_date": "2018-06-12T18:30:00.000Z",
                        "timeline": "3 weeks",
                        "responsibility": "2",
                        "status": "Not Started"
                    },
                    {
                        "activity_id": 2,
                        "activity": "Territory re-alignment",
                        "start_date": "2018-06-27T18:30:00.000Z",
                        "timeline": "2 Weeks",
                        "responsibility": "2",
                        "status": "Not Started"
                    }
                ]
            }
        ]

    },
    {
        "id": 2,
        "milestone": "Identified",
        "phase":[
            {
                "phase_id": 2,
                "phase": "Pilot Planning",
                "activity"[
                    {
                        "activity_id": 3,
                        "activity": "Existing ICP Discussion",
                        "start_date": "2018-06-27T18:30:00.000Z",
                        "timeline": "2 Weeks",
                        "responsibility": "2",
                        "status": "Not Started"
                    }
                ]
            }
        ]

    }
]

使用node.js。只希望一个简单的解决方案可能是使用for循环。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

可以使用数组reduce方法进行救援

流程如下。在数组reduce中,首先在thisArg中传递一个空数组。在空数组中,检查主数组中是否存在ID为id的对象。如果不存在,则创建一个新对象并推送到空数组< / p>

let newData = date.reduce(function (acc, curr) {
  //using findIndex to test if there exist an object whose id is same as id of main 
 //array of objects
  let getIdIndex = acc.findIndex(function (item) {
    return item.id === curr.id;
  })
  // if the id does not exist then create a new object with these values
  if (getIdIndex === -1) {
    let newObj = {
      id: curr.id,
      milestone: curr.milestone,
      phase: [{
        phase_id: curr.phase_id,
        phase: curr.phase,
        activity: [{
          activity_id: curr.activity_id,
          activity: curr.activity,
          start_date: curr.start_date,
          timeline: curr.timeline,
          responsibility: curr.responsibility,
          status: curr.status
        }]

      }]


    }
    acc.push(newObj)
  }
  else{
    // now if the id exist you need to check if therse exist an object
    // where phase id matches, then same as the above logic
    let getPhaseIdIndex = acc[getIdIndex].phase.findIndex(function(item){
         return item.phase_id === curr.phase_id;
    })
    if(getPhaseIdIndex === -1){
      acc[getIdIndex].phase.push({
        phase_id: curr.phase_id,
        phase: curr.phase,
        activity: [{
          activity_id: curr.activity_id,
          activity: curr.activity,
          start_date: curr.start_date,
          timeline: curr.timeline,
          responsibility: curr.responsibility,
          status: curr.status
        }]

      })
    }
    else{
      acc[getIdIndex].phase[getPhaseIdIndex].activity.push(
        {
          activity_id: curr.activity_id,
          activity: curr.activity,
          start_date: curr.start_date,
          timeline: curr.timeline,
          responsibility: curr.responsibility,
          status: curr.status
        }
      )
    }
}
return acc;
}, []) // this empty array is thisArg, new values will be added to this
console.log(newData)

使用工作演示here

获取完整的代码