我有一個回傳多張作業表的 notifications.find 查詢(貓鼬)
Notification.find({id: id}, function(err, notifications){}
我需要像這樣轉換回傳的時間戳值
[
{
_id: new ObjectId("12934193c51a231b0165425a"),
userid: '62921df1c14a2eea0efa9399',
timestamp: 1653817696599,
},
{
_id: new ObjectId("11934193c51a231b0165425a"),
userid: '62921df1c14a2eea0efa9399',
timestamp: 1653817696600,
}
]
我試過這個
notifications.forEach((element, index) => {
notifications[index].timestamp = new Date(notifications[index].timestamp);
});
和這段代碼
new Date(notifications[index].timestamp);
似乎可以通過轉換每個值的時間戳來作業,但我無法在陣列中替換它
我已經在這個問題上討論了幾個小時
uj5u.com熱心網友回復:
而不是forEach使用map
const newNotifications = notifications.map((element, index) => {
return {
...element,
timestamp: new Date(element.timestamp),
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/486415.html
標籤:javascript 节点.js 数组 mongodb 猫鼬
