Así es como se ve mi matriz original
[ { _id: new ObjectId("12934193c51a231b0165425a"), userid: '62921df1c14a2eea0efa9399', timestamp: 1653817696599, }, { _id: new ObjectId("11934193c51a231b0165425a"), userid: '62921df1c14a2eea0efa9399', timestamp: 1653817696600, } ]Yo uso este código para convertir la marca de tiempo
const newNotifications = notifications.map((element, index) => { return { element: element, new_timestamp: new Date(element.timestamp), }; });Y este es el resultado que tengo
[ { "element": { "_id": "12934193c51a231b0165425a", "userid": "62921df1c14a2eea0efa9399", "timestamp": 1653817696599, }, "timestamp": "2022-05-29T09:48:16.599Z", }, ]Intento que se vea así
[ { _id: new ObjectId("12934193c51a231b0165425a"), userid: '62921df1c14a2eea0efa9399', timestamp: 1653817696599, new_timestamp: '2022-05-29T09:48:16.599Z', }, { _id: new ObjectId("11934193c51a231b0165425a"), userid: '62921df1c14a2eea0efa9399', timestamp: 1653817696600, new_timestamp: '2022-05-29T09:48:16.599Z', }, ] Me gustaría que la marca de tiempo sea parte de cada objeto recién creado y elimine la propiedad del element generada por el mapeo actual.
Puede vincular new_timestamp en el objeto existente y luego devolver el objeto completo :
const notifications = [ { _id: "12934193c51a231b0165425a", userid: '62921df1c14a2eea0efa9399', timestamp: 1653817696599, }, { _id: "11934193c51a231b0165425a", userid: '62921df1c14a2eea0efa9399', timestamp: 1653817696600, } ]; const newNotifications = notifications.map((element) => { element['new_timestamp'] = new Date(element.timestamp).toString() return element; }); console.log(newNotifications);