I have a Array in multiple type e.g Time-In or Time-Out , Break-in ,Break-out etc. here is list
const data =
[ {
, entryDateTime : '02:28'
, activityTypeId : 'Time In'
, comments : 'dgdfgdfg'
, isLeave : ''
}
, { timeSheetDetailActivityId : 0
, timeSheetDailyActivityId : 0
, entryDateTime : '03:28'
, activityTypeId : 'Time Out'
, comments : '2323'
, isLeave : ''
}
, {
, entryDateTime : '04:28'
, activityTypeId : 'Break In'
, comments : '2323'
, isLeave : ''
}
, { timeSheetDetailActivityId : 0
, timeSheetDailyActivityId : 0
, entryDateTime : '05:28'
, activityTypeId : 'Break Out'
, comments : '2323'
, isLeave : ''
}
, {
, entryDateTime : '06:28'
, activityTypeId : 'Time In'
, comments : '2323'
, isLeave : ''
}
, {
, entryDateTime : '07:28'
, activityTypeId : 'Time Out'
, comments : '232323'
, isLeave : ''
}
]
i want above list to like this
[ {
, Time In : '02:28'
, Time Out : '03:28'
, Break In : '02:28'
, Break Out : '03:28'
}
, {
, Time In : '06:28'
, Time Out : '08:28'
, Break In : '02:28'
, Break Out : '03:28'
}
]
how is possible?
i ll be thanks full to all am stuck from 2 days Please help me or give some time on it. Thanks in advance.
Sure, piece of cake.
Build an object that keeps track of your data per activity ID, then read it.
const groupedData = {};
data.forEach(({ timeSheetDailyActivityId, activityTypeId, entryDateTime }) => {
// Retrieve group object from `groupedData`,
// initialize one (assigning it back) if it doesn't yet exist.
const group =
groupedData[timeSheetDailyActivityId] ||
(groupedData[timeSheetDailyActivityId] = { timeSheetDailyActivityId });
// Add the activity->datetime mapping to the group.
group[activityTypeId] = entryDateTime;
});
// Since `groupedData` is an object and we want an array, use `Object.values`.
console.log(Object.values(groupedData));
This prints out e.g. (after changing the example input data a bit so there are multiple dailyIds):
[
{
timeSheetDailyActivityId: 0,
'Time In': '02:28',
'Time Out': '03:28',
'Break In': '04:28',
'Break Out': '05:28'
},
{
timeSheetDailyActivityId: 1,
'Time In': '06:28',
'Time Out': '07:28'
}
]