实际上,我有一个对象列表,输出如下:
[
{
"eventDate": "August 15, 2022 at 9:00 AM",
"title": "Dr.",
"first_name": "Jane",
"last_name": "Doe"
},
{
"eventDate": "August 15, 2022 at 1:00 AM",
"title": "Dr.",
"first_name": "Jane",
"last_name": "Doe"
}
]
从本质上说,正如您在上面看到的,结果几乎相同,除了eventDate
中列出的时间。时间对我来说没有区别,我想消除基本相同的重复条目,就像上面一样,唯一可以不必匹配的是时间。
数据以字符串形式保存在数据库中,因此不像更改日期格式那么简单。因此,到目前为止,我所做的如下(对于第一个条目):
var fullDate = res.items[0].sessionStartTime
console.log(fullDate);
var month = fullDate.split(' ')[0];
var day = fullDate.split(' ')[1];
var year = fullDate.split(' ')[2];
var newDate = month + " " + day + " " + year
var formattedDate = new Date(newDate).toDateString();
console.log(formattedDate);
其输出formattedDate
如下:Mon Aug 15 2022
我想要做的是将数组中的每个eventDate
条目转换为新的日期格式输出,因此它看起来如下所示:
newArray =
[
{
"eventDate": "Mon Aug 15 2022",
"title": "Dr.",
"first_name": "Jane",
"last_name": "Doe"
},
{
"eventDate": "Mon Aug 15 2022",
"title": "Dr.",
"first_name": "Jane",
"last_name": "Doe"
}
]
因此,这样我就可以通过使用以下函数找到eventDate
的不同值:
const unique = [...new Map(newArray.map((m) => [m.eventDate, m])).values()];
其应输出:
unique =
[
{
"eventDate": "Mon Aug 15 2022",
"title": "Dr.",
"first_name": "Jane",
"last_name": "Doe"
}
]
谢谢!
编辑:
我的实际对象列表是:
[
{
formField7: "Test",
amountDue: "Test",
serviceType: "Course",
staffMember: "Test",
formField2: "Test",
formResponse2: "-",
formResponse7: "Test",
formField6: "Test",
formField10: "Test",
paymentStatus: "Test",
formField1: "Test",
dateOfBooking: "Test",
_id: "Test",
_owner: "Test",
_createdDate: "Test",
serviceName: "Test",
formField8: "Test",
paymentMethod: "Test",
groupSize: "1",
formField4: "Test",
formResponse1: "Test",
formField5: "Test",
_updatedDate: "Test",
formResponse5: "Test",
eventDate: "August 15, 2022 at 9:00 AM",
formResponse8: "Test",
couponName: "Test",
formResponse0: "Test",
formResponse11: "Test",
clientEmail: "Test",
formField9: "Test",
attendanceStatus: "Test",
sessionDuration: "Test",
formField3: "Test",
formResponse4: "-",
clientAddress: "-",
formResponse9: "Test",
clientName: "Jane Doe",
formField11: "Test",
formResponse6: "Test",
formField0: "Test",
bookingStatus: "Test",
clientPhoneNumber: "Test",
amountPaid: "Test",
formResponse10: "Test",
formResponse3: "Test"
},
{
formField7: "Test",
amountDue: "Test",
serviceType: "Course",
staffMember: "Test",
formField2: "Test",
formResponse2: "-",
formResponse7: "Test",
formField6: "Test",
formField10: "Test",
paymentStatus: "Test",
formField1: "Test",
dateOfBooking: "Test",
_id: "Test",
_owner: "Test",
_createdDate: "Test",
serviceName: "Test",
formField8: "Test",
paymentMethod: "Test",
groupSize: "1",
formField4: "Test",
formResponse1: "Test",
formField5: "Test",
_updatedDate: "Test",
formResponse5: "Test",
eventDate: "August 15, 2022 at 1:00 AM",
formResponse8: "Test",
couponName: "Test",
formResponse0: "Test",
formResponse11: "Test",
clientEmail: "Test",
formField9: "Test",
attendanceStatus: "Test",
sessionDuration: "Test",
formField3: "Test",
formResponse4: "-",
clientAddress: "-",
formResponse9: "Test",
clientName: "Jane Doe",
formField11: "Test",
formResponse6: "Test",
formField0: "Test",
bookingStatus: "Test",
clientPhoneNumber: "Test",
amountPaid: "Test",
formResponse10: "Test",
formResponse3: "Test"
}
]
在数据库上运行查询后,将其输出为JSON。
1条答案
按热度按时间yr9zkbsy1#
我的解决办法是
1.遍历数组,
1.转换日期,
1.通过基于事件中的所有属性创建唯一属性,将事件添加到对象,以及
1.将对象转换为数组