需要按日期筛选数据



我有一些查询结果数据,比如。。

"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"
}
]

现在我需要过滤数据,如:

[
{
title: 2022-04-10 (here createdAt value),
value: {
"activityId": "5",
"universityId": "17",
"studentId": "25",
"content": "Add course in cart",
"activityType": "chat"
}
}
]

我正在使用nestJS。

const allActivity = [];
Promise.all(
results.map((value) => {
if (allActivity.length <= 0) {
const obj = {
title: value.createdAt,
values: value,
};
allActivity.push(obj);
}
}),
);

我只需要用这种格式过滤数据,或者只需要完成我在下面写的函数。

您可以简单地使用

select * from data where createdAt like '2022-04-10%'

带有此查询的返回表将采用与您需要的格式相同的格式。

尝试此映射函数

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)

您可以使用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)

使用groupBy更简单,但我使用的不是title,而是用于日期表示的密钥

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)

假设您想通过修剪时间按createdAt分组。

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);

最新更新