2

I have seen this, but it does not solve my problem: How do I group items in an array by date?

I want to arrange this chat message by date, I have successfully retrieved the message from the database using AJAX.

Here is the retrieved chat in the console.

[
    [
        "Paul Abioro",
        "paulexv....@gmail.com",
        "chester",
        "Teejay",
        "Hi Teejay",
        "3",
        "2023-04-06 17:42:54"
    ],
    [
        "Paul Abioro",
        "paulexvi....@gmail.com",
        "chester",
        "Teejay",
        "How are you doing?",
        "3",
        "2023-04-06 17:42:58"
    ],
    [
        "Teejay Bello",
        "tee....@gmail.com",
        "Teejay",
        "chester",
        "I'm Fine",
        "2",
        "2023-04-06 19:00:42"
    ],
    [
        "Paul Abioro",
        "paulexv....@gmail.com",
        "chester",
        "Teejay",
        "Testing",
        "3",
        "2023-04-08 10:12:38"
    ],
    [
        "Teejay Bello",
        "tee....@gmail.com",
        "Teejay",
        "Chester",
        "Testing what?",
        "2",
        "2023-04-08 10:21:09"
    ],
    [
        "Paul Abioro",
        "paul....@gmail.com",
        "chester",
        "Teejay",
        "How can we help you?",
        "3",
        "2023-04-08 10:38:25"
    ]
]

What I want to do



[
  date": "2023-04-06",
  chat":[
        "Paul Abioro",
        "paulexv....@gmail.com",
        "chester",
        "Teejay",
        "Hi Teejay",
        "3",
        "2023-04-06 17:42:54"
    ],
    [
        "Paul Abioro",
        "paulexvi....@gmail.com",
        "chester",
        "Teejay",
        "How are you doing?",
        "3",
        "2023-04-06 17:42:58"
    ],
    [
        "Teejay Bello",
        "tee....@gmail.com",
        "Teejay",
        "chester",
        "I'm Fine",
        "2",
        "2023-04-06 19:00:42"
    ],

 date": "2023-04-08",
   chat":[
        "Paul Abioro",
        "paulexv....@gmail.com",
        "chester",
        "Teejay",
        "Testing",
        "3",
        "2023-04-08 10:12:38"
    ],
    [
        "Teejay Bello",
        "tee....@gmail.com",
        "Teejay",
        "Chester",
        "Testing what?",
        "2",
        "2023-04-08 10:21:09"
    ],
    [
        "Paul Abioro",
        "paul....@gmail.com",
        "chester",
        "Teejay",
        "How can we help you?",
        "3",
        "2023-04-08 10:38:25"
    ]
]


I hope you get the idea. Thanks!

devsam247
  • 1,280
  • 13
  • 18

2 Answers2

1

you can use array reduce to group into an object and then take the values array of the reduced object using Object.values.
If you want the object grouped by date instead of the array remove the Object.values

the date portion can be obtained using
curr[curr.length-1].split(" ")[0]
or
curr.at(-1).split(" ")[0]
if your environment supports at

const a = [    [        "Paul Abioro",        "paulexv....@gmail.com",        "chester",        "Teejay",        "Hi Teejay",        "3",        "2023-04-06 17:42:54"    ],    [        "Paul Abioro",        "paulexvi....@gmail.com",        "chester",        "Teejay",        "How are you doing?",        "3",        "2023-04-06 17:42:58"    ],    [        "Teejay Bello",        "tee....@gmail.com",        "Teejay",        "chester",        "I'm Fine",        "2",        "2023-04-06 19:00:42"    ],    [        "Paul Abioro",        "paulexv....@gmail.com",        "chester",        "Teejay",        "Testing",        "3",        "2023-04-08 10:12:38"    ],    [        "Teejay Bello",        "tee....@gmail.com",        "Teejay",        "Chester",        "Testing what?",        "2",        "2023-04-08 10:21:09"    ],    [        "Paul Abioro",        "paul....@gmail.com",        "chester",        "Teejay",        "How can we help you?",        "3",        "2023-04-08 10:38:25"    ]]

const res = Object.values(a.reduce((acc,curr) => {
  const date = curr[curr.length-1].split(" ")[0]
  acc[date]??={date,chat:[]} //or acc[date] = acc[date] || {date,chat:[]}
  acc[date].chat.push(curr)
  return acc
},{}))

console.log(res)
cmgchess
  • 7,996
  • 37
  • 44
  • 62
  • Thanks for the code, however, I am trying to loop through each array using a nested while loop 1. first loop for the dates, 2. the nested loop for the arrays in each date. can you help me with that? – devsam247 Apr 08 '23 at 14:54
  • hi whats causing the issue. also better ask a new question if it is out of scope of this question so other people also can see – cmgchess Apr 08 '23 at 15:16
  • Okay, I did it. with forEach statement, Thanks @cmgchess – devsam247 Apr 08 '23 at 15:49
1

You can use reduce to get the result:

const chatArray = [
    [
        "Paul Abioro",
        "paulexv....@gmail.com",
        "chester",
        "Teejay",
        "Hi Teejay",
        "3",
        "2023-04-06 17:42:54"
    ],
    [
        "Paul Abioro",
        "paulexvi....@gmail.com",
        "chester",
        "Teejay",
        "How are you doing?",
        "3",
        "2023-04-06 17:42:58"
    ],
    [
        "Teejay Bello",
        "tee....@gmail.com",
        "Teejay",
        "chester",
        "I'm Fine",
        "2",
        "2023-04-06 19:00:42"
    ],
    [
        "Paul Abioro",
        "paulexv....@gmail.com",
        "chester",
        "Teejay",
        "Testing",
        "3",
        "2023-04-08 10:12:38"
    ],
    [
        "Teejay Bello",
        "tee....@gmail.com",
        "Teejay",
        "Chester",
        "Testing what?",
        "2",
        "2023-04-08 10:21:09"
    ],
    [
        "Paul Abioro",
        "paul....@gmail.com",
        "chester",
        "Teejay",
        "How can we help you?",
        "3",
        "2023-04-08 10:38:25"
    ]
]



const result = chatArray.reduce((acc, cur) => {
    const lastIndex = cur.length - 1;
    const date = cur[lastIndex]?.split(" ")[0];
    const chat = cur.slice(0, lastIndex);
    acc[date] = acc[date] ? [...acc[date], chat] : [chat];
    return acc;
  }, {});
  
const chatObjectsArr = Object.entries(result).map(([date, chat]) => ({ date, chat }));
  
console.log(chatObjectsArr);
protob
  • 3,317
  • 1
  • 8
  • 19