This is the current array of object I have, I want to transform this first array of object into the second one given below: I tried using reduce. I want that, for the particular date I should have all the topic as key and count as value :
```const myObj = [
{
"created_at": "2022-06-12T18:30:00.000Z",
"topic": "Technical_support/Installation",
"count": "2"
},
{
"created_at": "2022-06-10T18:30:00.000Z",
"topic": "pricing",
"count": "1"
},
{
"created_at": "2022-06-10T18:30:00.000Z",
"topic": "Product/sales enquiry",
"count": "1"
},
{
"created_at": "2022-06-10T18:30:00.000Z",
"topic": "Service",
"count": "4"
},
{
"created_at": "2022-06-10T18:30:00.000Z",
"topic": "others",
"count": "1"
},
{
"created_at": "2022-06-12T18:30:00.000Z",
"topic": "Product/sales enquiry",
"count": "5"
},
{
"created_at": "2022-06-11T18:30:00.000Z",
"topic": "Service",
"count": "28"
},
{
"created_at": "2022-06-11T18:30:00.000Z",
"topic": "Billing/order",
"count": "1"
},
{
"created_at": "2022-06-12T18:30:00.000Z",
"topic": "Service",
"count": "11"
},
{
"created_at": "2022-06-12T18:30:00.000Z",
"topic": "others",
"count": "5"
},
{
"created_at": "2022-06-10T18:30:00.000Z",
"topic": "Technical_support/Installation",
"count": "1"
},
{
"created_at": "2022-06-11T18:30:00.000Z",
"topic": "pricing",
"count": "6"
},
{
"created_at": "2022-06-12T18:30:00.000Z",
"topic": "pricing",
"count": "1"
},
{
"created_at": "2022-06-10T18:30:00.000Z",
"topic": "offer",
"count": "1"
},
{
"created_at": "2022-06-11T18:30:00.000Z",
"topic": "Product/sales enquiry",
"count": "4"
},
{
"created_at": "2022-06-11T18:30:00.000Z",
"topic": "offer",
"count": "1"
},
{
"created_at": "2022-06-11T18:30:00.000Z",
"topic": "others",
"count": "15"
},
{
"created_at": "2022-06-11T18:30:00.000Z",
"topic": "Technical_support/Installation",
"count": "10"
}
]
I want to achive this result I want to transform this first array of object into the second one given below: I tried using reduce. I want that, for the particular date I should have all the topic as key and count as value :
{
date: "2022-06-12T18:30:00.000Z",
"Billing / Order": 20,
Delivery: 30,
"Product/sales enquiry": 40,
Service: 24,
"Technical_support/Installation": 24,
pricing: 24,
Usability: 24,
offer: 24,
others: 24,
},
{
date: "2022-06-11T18:30:00.000Z",
"Billing/order": 30,
Delivery: 10,
"Product/sales enquiry": 30,
Service: 24,
"Technical_support/Installation": 14,
pricing: 54,
Usability: 24,
offer: 24,
others: 24,
},
{
date: "2022-06-12T18:30:00.000Z",
"Billing/order": 40,
Delivery: 30,
"Product/sales enquiry": 40,
Service: 54,
"Technical_support/Installation": 24,
pricing: 14,
Usability: 24,
offer: 14,
others: 34,
},
];```
CodePudding user response:
Based on your question,you can try below code snippet.
Note: the date
format is not translated as your posted
const myObj = [
{
"created_at": "2022-06-12T18:30:00.000Z",
"topic": "Technical_support/Installation",
"count": "2"
},
{
"created_at": "2022-06-10T18:30:00.000Z",
"topic": "pricing",
"count": "1"
},
{
"created_at": "2022-06-10T18:30:00.000Z",
"topic": "Product/sales enquiry",
"count": "1"
},
{
"created_at": "2022-06-10T18:30:00.000Z",
"topic": "Service",
"count": "4"
},
{
"created_at": "2022-06-10T18:30:00.000Z",
"topic": "others",
"count": "1"
},
{
"created_at": "2022-06-12T18:30:00.000Z",
"topic": "Product/sales enquiry",
"count": "5"
},
{
"created_at": "2022-06-11T18:30:00.000Z",
"topic": "Service",
"count": "28"
},
{
"created_at": "2022-06-11T18:30:00.000Z",
"topic": "Billing/order",
"count": "1"
},
{
"created_at": "2022-06-12T18:30:00.000Z",
"topic": "Service",
"count": "11"
},
{
"created_at": "2022-06-12T18:30:00.000Z",
"topic": "others",
"count": "5"
},
{
"created_at": "2022-06-10T18:30:00.000Z",
"topic": "Technical_support/Installation",
"count": "1"
},
{
"created_at": "2022-06-11T18:30:00.000Z",
"topic": "pricing",
"count": "6"
},
{
"created_at": "2022-06-12T18:30:00.000Z",
"topic": "pricing",
"count": "1"
},
{
"created_at": "2022-06-10T18:30:00.000Z",
"topic": "offer",
"count": "1"
},
{
"created_at": "2022-06-11T18:30:00.000Z",
"topic": "Product/sales enquiry",
"count": "4"
},
{
"created_at": "2022-06-11T18:30:00.000Z",
"topic": "offer",
"count": "1"
},
{
"created_at": "2022-06-11T18:30:00.000Z",
"topic": "others",
"count": "15"
},
{
"created_at": "2022-06-11T18:30:00.000Z",
"topic": "Technical_support/Installation",
"count": "10"
}
]
let result = myObj.reduce((a,v) =>{
let obj = a.find(i => i.date == v.created_at)
if(obj){
obj[v.topic] = v.count
}else{
obj = {'date':v.created_at,[v.tpoic]:v.count}
a.push(obj)
}
return a
},[])
console.log(result)