Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

127
Visualizações
How to merge two objects that are generated by a mapping process?

This is what my original array looks like

[                                                                                             
  {
    _id: new ObjectId("12934193c51a231b0165425a"),
    userid: '62921df1c14a2eea0efa9399',
    timestamp: 1653817696599,
  },
  {
    _id: new ObjectId("11934193c51a231b0165425a"),
    userid: '62921df1c14a2eea0efa9399',
    timestamp: 1653817696600,
  }
]

I use this code to convert the timestamp

const newNotifications = notifications.map((element, index) => {
  return {
    element: element,
    new_timestamp: new Date(element.timestamp),
  };
});

And this is the result I have

[
  {
    "element": {
      "_id": "12934193c51a231b0165425a",
      "userid": "62921df1c14a2eea0efa9399",
      "timestamp": 1653817696599,
    },
    "timestamp": "2022-05-29T09:48:16.599Z",
  },
]

I try to make it look like this

[                                                                                             
  {
    _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',
  },
]

I would like the timestamp to be part of each newly created object and remove the element property generated by the current mapping.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can bind the new_timestamp into the existing object nd then return the whole object :

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);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda