I have the json response with value week1,week2,week3,week4 in week key along with count based on category (meetingHash)and week wise.I tried to merge with .reduce but no luck.
Now creating 2 rows for the user if I have week1 and week2
for meetingHash 'Hold/Uncategorized'
for the user Sarathy Devaraju
in month may
.
But i want to combine as { meetingHash: "Hold/Uncategorized", month: "May", UserName: "Sarathy Devaraju", Week1: 2, Week2: 0, Week3: 3, Week4: 0 }
expected as
[{"UserName":"Sarathy Devaraju","month":"May","meetingHash":"Hold/Uncategorized","Week1":2,"Week2":0,"Week3":3,"Week4":0},
{"UserName":"Priya","month":"May","meetingHash":"Hold/Uncategorized","Week1":2,"Week2":0,"Week3":0,"Week4":0},
{"UserName":"Sarathy Devaraju","month":"May","meetingHash":"1st Presentation / Meeting","Week1":0,"Week2":1,"Week3":0,"Week4":0},
{"UserName":"Priya","month":"May","meetingHash":"1st Presentation / Meeting","Week1":0,"Week2":1,"Week3":0,"Week4":0},
{"UserName":"Sarathy Devaraju","month":"Jun","meetingHash":"1st Presentation / Meeting","Week1":0,"Week2":1,"Week3":0,"Week4":0},
{"UserName":"Priya","month":"Jun","meetingHash":"1st Presentation / Meeting","Week1":0,"Week2":1,"Week3":0,"Week4":0},
{"UserName":"Sarathy Devaraju","month":"Jun","meetingHash":"Follow-On Meetings","Week1":1,"Week2":0,"Week3":0,"Week4":0},
{"UserName":"Priya","month":"Jun","meetingHash":"Follow-On Meetings","Week1":1,"Week2":0,"Week3":0,"Week4":0},
{"UserName":"Sarathy Devaraju","month":"Jul","meetingHash":"Follow-On Meetings","Week1":0,"Week2":0,"Week3":1,"Week4":0},
{"UserName":"Sarathy Devaraju","month":"Jul","meetingHash":"Follow-On Meetings","Week1":1,"Week2":0,"Week3":0,"Week4":0},
]
const data = [
{
"UserName": "Sarathy Devaraju",
"week": "Week 1",
"MeetingHash": "Hold/Uncategorized",
"monthinwords": "May",
"count": 2
},
{
"UserName": "Sarathy Devaraju",
"week": "Week 2",
"MeetingHash": "Hold/Uncategorized",
"monthinwords": "May",
"count": 3
},
{
"UserName": "Sarathy Devaraju",
"week": "Week 2",
"MeetingHash": "1st Presentation / Meeting",
"monthinwords": "Jun",
"count": 1
},
{
"UserName": "Sarathy Devaraju",
"week": "Week 1",
"MeetingHash": "Follow-On Meetings",
"monthinwords": "Jun",
"count": 1
},
{
"UserName": "Sarathy Devaraju",
"week": "Week 2",
"MeetingHash": "1st Presentation / Meeting",
"monthinwords": "May",
"count": 1
},
{
"UserName": "Sarathy Devaraju",
"week": "Week 3",
"MeetingHash": "Follow-On Meetings",
"monthinwords": "July",
"count": 1
},
{
"UserName": "Priya",
"week": "Week 1",
"MeetingHash": "Hold/Uncategorized",
"monthinwords": "May",
"count": 2
},
{
"UserName": "Priya",
"week": "Week 2",
"MeetingHash": "1st Presentation / Meeting",
"monthinwords": "Jun",
"count": 1
},
{
"UserName": "Priya",
"week": "Week 1",
"MeetingHash": "Follow-On Meetings",
"monthinwords": "Jun",
"count": 1
},
{
"UserName": "Priya",
"week": "Week 2",
"MeetingHash": "1st Presentation / Meeting",
"monthinwords": "May",
"count": 1
},
{
"UserName": "Priya",
"week": "Week 1",
"MeetingHash": "Follow-On Meetings",
"monthinwords": "July",
"count": 1
}
];
const WEEKS = ["Week 1", "Week 2", "Week 3", "Week 4"];
const result = data.map(obj => {
return {
UserName: obj.UserName,
month: obj.monthinwords,
meetingHash: obj.MeetingHash,
Week1: WEEKS.indexOf(obj.week) == 0 ? obj.count : 0,
Week2: WEEKS.indexOf(obj.week) == 1 ? obj.count : 0,
Week3: WEEKS.indexOf(obj.week) == 2 ? obj.count : 0,
Week4: WEEKS.indexOf(obj.week) == 3 ? obj.count : 0,
}
}
);
console.log(result);