Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript : array manipulation

I work on chat app with react js This is my array of object

const messages = [ 
  {message: "ghhhhhhhh", receiver: "faty", sender: "harry", time: "10/1/2019 12:56"},
  {message: "ggggggghjjgcgh", receiver: "harry", sender: "marie", time: " 10/1/2019 12:45"},
  {message: "good afternoon", receiver: "harry", sender: "marie", time: " 10/1/2019 12:41"},
  {message: "hfdsghfdfhjo", receiver: "faty", sender: "harry", time: " 10/1/2019 12:38"},
  {message: "hhhhhhhhhhhhh ", receiver: "harry", sender: "faty", time: " 10/1/2019 11:50"}
];

I want to display the last message between each person

I try the code below but he doesn't render what i want

    const senders =messages.reduce((a, c) => {
      a[c.sender] = a[c.sender] || { data: [] };
      a[c.sender].data.push({ sender: c.sender, message: c.message, time: c.time, receiver: c.receiver, hotel_id: c.hotel_id, isseen: c.isseen });
      a[c.sender].data.sort((a, b) =>{console.log(new Date(b.time),'aaaaaaaaaaaaaa');return ( new Date(b.time).getTime() - new Date(a.time).getTime())})
      return a;
    }, {})
    var messageInbox = Object.values(senders).map(s => s.data[0])
    console.log("Latest messages:", messageInbox);

this what I want to display :

  {message: "ghhhhhhhh", receiver: "faty", sender: "harry", time: "10/1/2019 12:56"},
  {message: "ggggggghjjgcgh", receiver: "harry", sender: "marie", time: " 10/1/2019 12:45"}

let me explain I have two person faty and harry they have a lot of message between them i want to display the last message between them, the same between harry and marie

if you understand my problem please help me, thanks

like image 261
ch mariem Avatar asked May 17 '26 19:05

ch mariem


1 Answers

If you don't care whether a pair of recipients are a sender or receiver (so Hary could be the receiver/sender, and Faty vice versa) -- and it seems you don't care, based on your expected output -- then the code below should work. First I sort the messages in reverse time order, because then when iterating with reduce, the first unique recipient combination will be the last message between them.

Then I make unique keys for recipient pairs, regardless of whether they are receiver or sender, by adding them to an array, sorting that array, and JSON stringify-ing it. Sorting is what makes it such that you will get the last message between recipients, regardless of who was the receiver and who was the sender -- if you want two entries for Hary and Faty, one each for whether they are receiver or sender, then remove the sort.

If the object I'm returning on each iteration of reduce does not already have that key, I add the message using that key; if it does have that entry, I skip it as we already have the last message for that particular recipient pair. Finally I call Object.values on the object returned by reduce.

The code is also at this repl: https://repl.it/@dexygen/lastMessagesBetweenRecipientPairs

messages.sort((a,b)=>{new Date(b.time) - new Date(a.time)}); // reverse time order

const lastMessagesBetweenRecipientPairs = messages.reduce((lastMessages, msg) => {
    let key = JSON.stringify([msg.sender, msg.receiver].sort());
    if (!lastMessages[key]) lastMessages[key] = msg;
    return lastMessages;
}, {});

console.log(Object.values(lastMessagesBetweenRecipientPairs));

OUTPUT:

[ { message: 'ghhhhhhhh',
    receiver: 'faty',
    sender: 'harry',
    time: '10/1/2019 12:56' },
  { message: 'ggggggghjjgcgh',
    receiver: 'harry',
    sender: 'marie',
    time: ' 10/1/2019 12:45' } ]
like image 196
Dexygen Avatar answered May 19 '26 10:05

Dexygen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!