Converting an array of objects to array of objects with lesser fields [duplicate]

I have an array of objects let’s say processDetails. I want to convert it into an array of objects where each object has only two fields instead of every other field. How do I go about it?

"processDetails" : [ 
        {
            "process": "details",
            "stage": "done",
            "status": "collected",
            "updatedAt" : ISODate("2017-11-04T04:53:54.623Z"),
            "updatedBy" : "BuubebcikkMdFo5Np",
            "history" : [ 
                {
                    "status": "pending",
                    "createdAt" : ISODate("2017-11-04T04:53:54.257Z"),
                    "createdBy": "BuubebcikkMdFo5Np",
                    "assignedTo" : "BuubebcikkMdFo5Np"
                }, 
                {
                    "status": "collected",
                    "notes": null,
                    "createdAt" : ISODate("2017-11-04T04:53:54.623Z"),
                    "createdBy": "BuubebcikkMdFo5Np",
                    "assignedTo": "BuubebcikkMdFo5Np"
                }
            ],
            "incomplete": null
        }
]
"processDetails" : [ 
        {
            "process": "details",
            "stage": "done"
        }
]

1

You can try this one:

const processStage = processDetails.map((item) => {
        return {
          process: item.process,
          stage: item.stage
        }
      })

You can try this :

processDetails.map((item) => {
  return {
    'process':item.process,
    'stage':item.state
  }
});

New contributor

Karunia Leo Gultom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

use map function like this:

let newData = data.map((item)=>{return{"process":item.process,"stage":item.stage}})

Try this:

processDetails.map(detail => ({
    process: detail.process, 
    stage: detail.stage
}))

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *