Roy",
"doctorId": "1234",
"specialityCode": "GENMED",
"specialityName": "General Medicine",
"specialityId": "201517"
},
{
"doctorName": "Dr. Atmaram",
"doctorId": "1235",
"specialityCode": "CARDIO",
"specialityName": "Cardiology",
"specialityId": "201519"
},
{
"doctorName": "Dr. Dinesh",
"doctorId": "1236",
"specialityCode": "CARDIO",
"specialityName": "Cardiology",
"specialityId": "201519"
},
{
"doctorName": "Dr. Mangala",
"doctorId": "1237",
"specialityCode": "GENMED",
"specialityName": "General Medicine",
"specialityId": "201517"
},
{
"doctorName": "Dr. Smitha",
"doctorId": "1238",
"specialityCode": "ANES",
"specialityName": "Anaesthesiology",
"specialityId": "201518"
}
]
let result = [... new Set(doctors.map(speciality=>speciality.specialityName))].map(speciality=>({"name":speciality,"doctors":[]}));
doctors.forEach(x=>result.find(y=>y.name===x.specialityName).doctors.push(x));
console.log(JSON.stringify(result,null,3))Here I'm grouping doctors based on their specialties, into the result array
So far I've done like this.
Is there a better way to group these ???
processing....
const map = new Map(); for (const ele of doctors) { if (!map.has(ele.specialityName)) map.set(ele.specialityName, []); map.set(ele.specialityName, [...map.get(ele.specialityName), ele]); } console.log(Object.fromEntries(map.entries()));
Cool. this was very handy
Обсуждают сегодня