Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

256
Vistas
Update nested array ES6, JavaScript

I have the following array of objects:

[{
  idChatPublic: "1",
  message: "hello",
  chatLike: [{
    id: "1",
    idChatPublic: "1"
  }]
}]

What I want is simply add a new object into chatLike array. Here is my attempt, but it doesn't seem to be working whats wrong with this piece of code?

async function sendLike(messageId: string) {
  const newLike = {
    idChatPublic: messageId,
  }

  mutateMessages(
    (data) => {
      console.log(data) // returns the array I want to update
      data.map((message) => {
        if (message.idChatPublic === messageId) {
          console.log(message.chatLike) // returns the array inside the object I want to update
          return {
            ...message,
            chatLike: [...message.chatLike, newLike]
          }
        } else {
          return message
        }
      })
    }
  )
}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

Probably, you have to create const with new array and return it:

const newData = data.map((message) => {
  if (message.idChatPublic === messageId) {
    console.log(message.chatLike) // returns the array inside the object I want to update
    return {
      ...message,
      chatLike: [...message.chatLike, newLike]
    }
  } else {
    return message
  }
});

return newData;
about 4 years ago · Juan Pablo Isaza Denunciar

0

You don't need map(). I think you can do that like this:

async function sendLike(messageId: string) {

 const newLike = {
   idChatPublic: messageId,
 };
 
 mutateMessages((data) => {
   data.forEach((message) => {
     if (message.idChatPublic === messageId) {
       message.chatLike.push(newLike);
     }
   }
 });
 
}

Loop throw your objects array with forEach() and if the id will match you can update chatLike array with push() to add a new newLike object.

about 4 years ago · Juan Pablo Isaza Denunciar

0

const data = [
  {
    idChatPublic: "1",
    message: "hello",
    chatLike: [
      {
        id: "1",
        idChatPublic: "1",
      },
    ],
  },
];

function updateChatLike() {
  return data.map((d) => {
    return {
      ...d,
      chatLike: [
        ...d.chatLike,
        {
          id: 2,
          idChatPublic: "2",
        },
      ],
    };
  });
}

console.log(JSON.stringify(updateChatLike(), null, 4));

I have used JSON.stringify() to log complete nested object

Output

[
    {
        "idChatPublic": "1",
        "message": "hello",
        "chatLike": [
            {
                "id": "1",
                "idChatPublic": "1"
            },
            {
                "id": 2,
                "idChatPublic": "2"
            }
        ]
    }
]
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda