Reputation: 93
const form = [
{
"_id": "61436aa8deef0390dcfdea79",
"user": "613f92eb98da0c5facc1b4ca",
"registerName": "Edwin",
"email": "[email protected]",
"category": "Urusan Lain Lain",
"title": " Ah Yong",
"contactNumber": "0191231232",
--> "time": "8am",
"dateBooking": "2021-09-18",
"icNumber": "860909-49-5851",
"date": "2021-09-16T16:02:48.457Z",
"__v": 0
},
{
"_id": "61436ab8deef0390dcfdea7a",
"user": "613f92eb98da0c5facc1b4ca",
"registerName": "ESTSETS",
"email": "[email protected]",
"category": "Daftar Baru",
"title": " Ah Yong",
"contactNumber": "0149530130",
--> "time": "8am",
"dateBooking": "2021-09-18",
"icNumber": "123123123123",
"date": "2021-09-16T16:03:04.211Z",
"__v": 0
},
{
"_id": "61436b1894116c90f82170b9",
"user": "613f92eb98da0c5facc1b4ca",
"registerName": "Ah Yong",
"email": "[email protected]",
"category": "Pertanyaan",
"title": " Ah Yong",
"contactNumber": "0149530130",
--> "time": "9am]",
"dateBooking": "2021-09-18",
"icNumber": "123654",
"date": "2021-09-16T16:04:40.962Z",
"__v": 0
}
]
How can i convert time
inside the object into
uniqueCount = [8am,9am,10am,8am,9am,10am,8am,9am,10am,11am];
and count the frequency of each time in the array? I want to have a result like:
8am = 3
9am = 3
10am = 3
11am = 1
I'm not sure whether this will work cause I would like to filter each time got how many people to create an condition.
Upvotes: 0
Views: 182
Reputation: 89404
You can use Array#map
to get an array of the time properties and then use Array#reduce
to get an object with the frequencies.
const arr=[{_id:"61436aa8deef0390dcfdea79",user:"613f92eb98da0c5facc1b4ca",registerName:"Edwin",email:"[email protected]",category:"Urusan Lain Lain",title:" Ah Yong",contactNumber:"0191231232",time:"8am",dateBooking:"2021-09-18",icNumber:"860909-49-5851",date:"2021-09-16T16:02:48.457Z",__v:0},{_id:"61436ab8deef0390dcfdea7a",user:"613f92eb98da0c5facc1b4ca",registerName:"ESTSETS",email:"[email protected]",category:"Daftar Baru",title:" Ah Yong",contactNumber:"0149530130",time:"8am",dateBooking:"2021-09-18",icNumber:"123123123123",date:"2021-09-16T16:03:04.211Z",__v:0},{_id:"61436b1894116c90f82170b9",user:"613f92eb98da0c5facc1b4ca",registerName:"Ah Yong",email:"[email protected]",category:"Pertanyaan",title:" Ah Yong",contactNumber:"0149530130",time:"9am",dateBooking:"2021-09-18",icNumber:"123654",date:"2021-09-16T16:04:40.962Z",__v:0}];
const res = arr.map(x => x.time).reduce((acc, curr)=>{
acc[curr] = (acc[curr] || 0) + 1;
return acc;
}, {});
console.log(res);
Upvotes: 2