简体   繁体   English

如何将数组 object 和 map 中的某些值的一部分从 AWS MongoDB 值分离到新数组中?

[英]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 .在最后一行代码中,我想从allUserListuserBirthday中获取日期和月份值,并与thisDaythisMonth进行比较。 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.

相关问题 无法映射到从 mongodb 获取的数组中的对象值 - Not able to map to values of the object inside an array fetched from mongodb 如何从一个数组和一个对象中.map()一个新的值数组? - How do I .map() a new array of values from an array and an object? 如何将数组中的值映射到对象数组? - How to map values from an array to an array of Object? map 值到 object 内的数组 - map values to an array inside an object Map 数组 object 值到新数组 - Map array object values to new array 如何将对象属性的值数组更新为新值 mongodb? - How to update array of object property's value to new value mongodb? 如何将 object 的 map 数组转换为 mongodb 中的数字? - How to map array of object and convert string value to number in mongodb? 如何使用父对象数组中子对象数组的唯一值有效地构造新对象数组 - How to efficiently construct new object array using unique values from arrays of children inside an array of parent objects 如何使用mongodb删除数组内的对象,数组内的对象 - How to delete an object inside an array, inside an object inside an array with mongodb 当它具有某些属性值时,如何从另一个数组中的数组中删除一些完整的 object? - How could I remove some entire object from an array inside another array when it has some property value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM