[英]How to detach a part of some value inside a array object and map into new array from AWS MongoDB values?
Actually, I tried to map values (userID, userName & userBirthday) into an array object from the AWS MongoDB and I want to compare is the user's birthday is today or not.实际上,我尝试将来自 AWS MongoDB 的 map 值(用户 ID、用户名和用户生日)放入数组 object 中,我想比较用户的生日是不是今天。 In MongoDB, the user's birthday (column name; dateOfBirth ) is stored in this format.在 MongoDB 中,用户的生日(列名; dateOfBirth )以这种格式存储。 "YYYY-MM-DDT00:00:00" So I want to get only month value & date value from this. "YYYY-MM-DDT00:00:00" 所以我只想从中获取月份值和日期值。 But I'm unable to split this value from the array.但我无法从数组中拆分这个值。 How it can or is any other way to do that?它怎么能或有其他方法可以做到这一点? I'm trouble with build this logic.我很难建立这个逻辑。 So help would be much appreciated!所以帮助将不胜感激!
const allUsers = await helper.getAllUsers(organizationId); //get all user data
const allUserList = allUsers.Items.map(user => (
{ userId: user.id,
userFullName: `${user.firstName} ${user.lastName}`,
userBirthday: user.dateOfBirth
}));
const thisDay = new Date().getMonth();
const thisMonth = new Date().getDate();
const birthdayUserList = allUserList.filter(
user => ??????? === thisDay && ??????? === thisMonth
);
In the last code-line, I want to get the date & month value from userBirthday of the allUserList & compare with thisDay & thisMonth .在最后一行代码中,我想从allUserList的userBirthday中获取日期和月份值,并与thisDay和thisMonth进行比较。 Furthermore, I attached the inside look of the allUserList此外,我附上了allUserList的内部外观
[
{ userId: 'xxxxxxxxxxxxxxx', userFullName: 'xxxxx xxxx' }, userBirthday: 'YYYY-MM-DDT00:00:00'},
{ userId: 'xxxxxxxxxxxxxxx', userFullName: 'xxxxx xxxx' }, userBirthday: 'YYYY-MM-DDT00:00:00'},
{ userId: 'xxxxxxxxxxxxxxx', userFullName: 'xxxxx xxxx' }, userBirthday: 'YYYY-MM-DDT00:00:00'},
{ userId: 'xxxxxxxxxxxxxxx', userFullName: 'xxxxx xxxx' }, userBirthday: 'YYYY-MM-DDT00:00:00'},
.....
]
const allUsers = await helper.getAllUsers(organizationId); //get all user data
const allUserList = allUsers.Items.map(user => (
{ userId: user.id,
userFullName: `${user.firstName} ${user.lastName}`,
userBirthMonth: new Date(user.dateOfBirth).getMonth(),
userBirthDate: new Date(user.dateOfBirth).getDate()
}));
const thisDay = new Date().getMonth();
const thisMonth = new Date().getDate();
const birthdayUserList = allUserList.filter(user =>
user.userBirthMonth === currentMonth && user.userBirthDate === currentDate
);
or you can use the month & date together as follows.或者您可以按如下方式一起使用月份和日期。
{ userBirthday: `${new Date(user.dateOfBirth).getMonth()}-${new Date(user.dateOfBirth).getDate()} }
in the allUserList
& you should store the current date as mentioned formate of above.在allUserList
中,您应该按照上面提到的格式存储当前日期。
const currentDate = `${new Date().getMonth()}-${new Date().getDate()}`
Logic can use as follows.逻辑可以使用如下。
const birthdayUserList = allUserList.filter( user => user.userBirthday === currentDate);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.