I have some query result data like..
"results": [
{
"activityId": "7",
"universityId": "23",
"studentId": "25",
"content": "Add course in cart",
"activityType": "chat",
"createdAt": "2022-04-10T22:25:29.798Z"
},
{
"activityId": "6",
"universityId": "17",
"studentId": "25",
"content": "Add course in cart",
"activityType": "chat",
"createdAt": "2022-04-09T23:48:23.554Z"
},
{
"activityId": "5",
"universityId": "17",
"studentId": "25",
"content": "Add course in cart",
"activityType": "chat",
"createdAt": "2022-04-08T23:48:21.841Z"
}
]
Now I need to filter the data like:
[
{
title: 2022-04-10 (here createdAt value),
value: {
"activityId": "5",
"universityId": "17",
"studentId": "25",
"content": "Add course in cart",
"activityType": "chat"
}
}
]
I am using nestJS.
const allActivity = [];
Promise.all(
results.map((value) => {
if (allActivity.length <= 0) {
const obj = {
title: value.createdAt,
values: value,
};
allActivity.push(obj);
}
}),
);
I just need to filter the data with this format or just need to complete my function that I have written below.
CodePudding user response:
You can check this implementation with reduce
const results = [{
"activityId": "7",
"universityId": "23",
"studentId": "25",
"content": "Add course in cart",
"activityType": "chat",
"createdAt": "2022-04-10T22:25:29.798Z"
},
{
"activityId": "6",
"universityId": "17",
"studentId": "25",
"content": "Add course in cart",
"activityType": "chat",
"createdAt": "2022-04-09T23:48:23.554Z"
},
{
"activityId": "5",
"universityId": "17",
"studentId": "25",
"content": "Add course in cart",
"activityType": "chat",
"createdAt": "2022-04-08T23:48:21.841Z"
}, {
"activityId": "8",
"universityId": "17",
"studentId": "25",
"content": "New testing",
"activityType": "chat",
"createdAt": "2022-04-08T23:48:21.841Z"
}
]
//format your date from `2022-04-08T23:48:21.841Z` to `2022-04-08`
const formatDate = (date) => date.split('T')[0]
const finalResult = results
.reduce((finalList, item) => {
const currentItemFormattedDate = formatDate(item.createdAt)
const foundItem = finalList.find(addedItem => formatDate(addedItem.title) === currentItemFormattedDate)
//if it's not in the final list, we need to create a new title
if (!foundItem) {
finalList.push({
title: currentItemFormattedDate,
value: [{
...item
}]
})
} else {
//if it's in the final list, we just need to push value
foundItem.value.push({
...item
})
}
return finalList
}, [])
console.log(finalResult)
The easier way with groupBy
, but instead of title
, I'm using keys for date representation
const results = [{
"activityId": "7",
"universityId": "23",
"studentId": "25",
"content": "Add course in cart",
"activityType": "chat",
"createdAt": "2022-04-10T22:25:29.798Z"
},
{
"activityId": "6",
"universityId": "17",
"studentId": "25",
"content": "Add course in cart",
"activityType": "chat",
"createdAt": "2022-04-09T23:48:23.554Z"
},
{
"activityId": "5",
"universityId": "17",
"studentId": "25",
"content": "Add course in cart",
"activityType": "chat",
"createdAt": "2022-04-08T23:48:21.841Z"
}, {
"activityId": "8",
"universityId": "17",
"studentId": "25",
"content": "New testing",
"activityType": "chat",
"createdAt": "2022-04-08T23:48:21.841Z"
}
]
const formatDate = (date) => date.split('T')[0]
//groupBy is not fully supported for all browsers, so I use vanilla Javascript for it
const groupBy = function(xs, key) {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
const finalResult = groupBy(results.map(item => ({...item, createdAt: formatDate(item.createdAt)})), "createdAt")
console.log(finalResult)
CodePudding user response:
You can simply use
select * from data where createdAt like '2022-04-10%'
The returned table with this query will be in the same format that you need.
CodePudding user response:
try this map function
const results = [{
"activityId": "7",
"universityId": "23",
"studentId": "25",
"content": "Add course in cart",
"activityType": "chat",
"createdAt": "2022-04-10T22:25:29.798Z"
},
{
"activityId": "6",
"universityId": "17",
"studentId": "25",
"content": "Add course in cart",
"activityType": "chat",
"createdAt": "2022-04-09T23:48:23.554Z"
},
{
"activityId": "5",
"universityId": "17",
"studentId": "25",
"content": "Add course in cart",
"activityType": "chat",
"createdAt": "2022-04-08T23:48:21.841Z"
}
]
const mapped = results.map(({
createdAt,
...value
}) => {
return {
title: createdAt.split('T')[0],
value
}
})
console.log(mapped)
CodePudding user response:
Assuming you want to group by createdAt
by trimming time.
const results = [{activityId:"6",universityId:"17",studentId:"25",content:"Add course in cart",activityType:"chat",createdAt:"2022-04-09T23:48:23.554Z"},{activityId:"5",universityId:"17",studentId:"25",content:"Add course in cart",activityType:"chat",createdAt:"2022-04-08T23:48:21.841Z"},{activityId:"9",universityId:"11",studentId:"26",content:"Add course in cart",activityType:"chat",createdAt:"2022-04-08T23:48:21.841Z"}];
const map = new Map();
for (const ele of results) {
const date = new Intl.DateTimeFormat('en-GB', {
dateStyle: 'medium',
}).format(new Date(ele.createdAt));
if (map.has(date)) {
map.set(date, [ele, ...map.get(date)]);
continue;
}
map.set(date, [ele]);
}
const output = Object.fromEntries(map.entries());
console.log(output);