Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

260
Views
Actualizar matriz anidada ES6, JavaScript

Tengo la siguiente matriz de objetos:

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

Lo que quiero es simplemente agregar un nuevo objeto en la matriz chatLike. Aquí está mi intento, pero no parece estar funcionando. ¿Qué pasa con este fragmento de código?

 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 answers
Answer question

0

El método map() crea una nueva matriz con los resultados de llamar a una función proporcionada en cada elemento de la matriz de llamadas.

Probablemente, debe crear const con una nueva matriz y devolverla:

 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 Report

0

No necesitas map() . Creo que puedes hacerlo así:

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

Repita la matriz de objetos con forEach() y, si la identificación coincide, puede actualizar la matriz chatLike con push() para agregar un nuevo objeto newLike .

about 4 years ago · Juan Pablo Isaza Report

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

He usado JSON.stringify() para registrar el objeto anidado completo

Producción

 [ { "idChatPublic": "1", "message": "hello", "chatLike": [ { "id": "1", "idChatPublic": "1" }, { "id": 2, "idChatPublic": "2" } ] } ]
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!