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

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

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 Report

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

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 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!