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

146
Views
how to get last object from array and remains parent array same in javascript

Hi I have an array with a messages key which have multiple objects, i want to remove all objects except the last object in the messages array

I have example array below :

const array = [
    {
      messages: [
        { content: "abc", id: "message01" },
        { content: "def", id: "message02" },
        { content: "ghi", id: "message03" }
      ],
      user: "user 1",
      participants: ["user1", "user2"],
      id: "chat01"
    },
    {
      messages: [
        { content: "hello", id: "message01" },
        { content: "hi", id: "message02" },
        { content: "How are you", id: "message03" }
      ],
      user: "user 2",
      participants: ["user1", "user2"],
      id: "chat02"
    }
  ];

my excepted array below:

const expectedArray = [
    {
      messages: [{ content: "ghi", id: "message03" }],
      user: "user 1",
      participants: ["user1", "user2"],
      id: "chat01"
    },
    {
      messages: [{ content: "How are you", id: "message03" }],
      user: "user 2",
      participants: ["user1", "user2"],
      id: "chat02"
    }
  ];
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Is this what you want?

EDIT: this code will loop the array, and set messages to messages[0] which is the last object

const array = [
  {
    messages: [
      {
        content: "abc",
        id: "message01"
      },
      {
        content: "def",
        id: "message02"
      },
      {
        content: "ghi",
        id: "message03"
      }
    ],
    user: "user 1",
    participants: ["user1", "user2"],
    id: "chat01"
  },
  {
    messages: [
      {
        content: "hello",
        id: "message01"
      },
      {
        content: "hi",
        id: "message02"
      },
      {
        content: "How are you",
        id: "message03"
      }
    ],
    user: "user 2",
    participants: ["user1", "user2"],
    id: "chat02"
  }
];

array.forEach(e=>{
  e.messages = e.messages[e.messages.length - 1]
})

console.log(array)

about 4 years ago · Juan Pablo Isaza Report

0

const array = [
    {
      messages: [
        { content: "abc", id: "message01" },
        { content: "def", id: "message02" },
        { content: "ghi", id: "message03" }
      ],
      user: "user 1",
      participants: ["user1", "user2"],
      id: "chat01"
    },
    {
      messages: [
        { content: "hello", id: "message01" },
        { content: "hi", id: "message02" },
        { content: "How are you", id: "message03" }
      ],
      user: "user 2",
      participants: ["user1", "user2"],
      id: "chat02"
    }
  ];
  

/** slice() method gets the last element of the array. 
If we provide negative index -1 to it
then it will remove the last item and
return it as a new array.
We overwrite messages array with the
return value of the slice() method which
is a new array containing only the last
element of the original messages array.
*/
array.forEach(e => e.messages = e.messages.slice(-1))

console.log(array)

about 4 years ago · Juan Pablo Isaza Report

0

const array = [
    {
      messages: [
        { content: "abc", id: "message01" },
        { content: "def", id: "message02" },
        { content: "ghi", id: "message03" }
      ],
      user: "user 1",
      participants: ["user1", "user2"],
      id: "chat01"
    },
    {
      messages: [
        { content: "hello", id: "message01" },
        { content: "hi", id: "message02" },
        { content: "How are you", id: "message03" }
      ],
      user: "user 2",
      participants: ["user1", "user2"],
      id: "chat02"
    }
  ];
  
  for (let i = 0; i < array.length; i++) {
    array[i].messages = new Array (array[i].messages[array[i].messages.length -1])
}

  
  console.log (array)

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!