2

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"
    }
  ];
2
  • i am unable to remove other objects from the messages array, except last object Commented Jan 27, 2022 at 14:03
  • 1
    use slice or splice Commented Jan 27, 2022 at 14:04

3 Answers 3

3

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)

Sign up to request clarification or add additional context in comments.

5 Comments

While your answer could be correct, write a brief description of what your code does is always a good idea. Code-only answer are usually downvoted (a claer example here stackoverflow.com/a/70876176/13897065 in which you wrote the correct answer that is downvoted). Just a suggestion. Have a nice coding =)
thank you so much, it will help me to help others, it's a great idea BTW, I'm editing it right now, but also don't forget this is my first week in stackoverflow
@ManoharPamishetty you're welcome dear, but please if it solves your issue please mark it as accepted so other visitors know that this is the solution of your issue
Nice @Sarout. To get the last element of the array, you could also use in your array.forEach loop instead of e.messages[e.messages.length - 1]: e.messages.pop(), e.messages.slice(-1).pop(), e.messages.slice(-1)[0]
source on how to get the last element of the array: w3docs.com/snippets/javascript/…
1

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)

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.