I have an array list and and I want only last record from the array list, how to get that?
I want message(i.e. Hey) from last record of this array.
here is my code:
let chats : any = [];
items.forEach((item) =>
{
if(item.val().sent_by == loggedInUserKey && item.val().sent_to == UserKey)
{
chats.push({
key : item.key,
sent_by : item.val().sent_by,
sent_to : item.val().sent_to,
message : item.val().message,
datetime : item.val().datetime
});
}
if(item.val().sent_to == loggedInUserKey && item.val().sent_by == UserKey)
{
chats.push({
key : item.key,
sent_by : item.val().sent_by,
sent_to : item.val().sent_to,
message : item.val().message,
datetime : item.val().datetime
});
}
});
this.chats = chats;
var last = chats[chats.length - 1];
console.log("last: ",last);
console.log("Chats : ",this.chats);
Please help. Thanks in advance.