这是我到目前为止的JavaScript代码:
var linkElement = document.getElementById("BackButton");
var loc_array = document.location.href.split('/');
var newT = document.createTextNode(unescape(capWords(loc_array[loc_array.length-2])));
linkElement.appendChild(newT);
Run Code Online (Sandbox Code Playgroud)
目前,它从URL中获取数组中倒数第二个项目.但是,我想检查数组中的最后一项是"index.html",如果是,请抓住第三项到最后一项.
我需要与以下相同的结果:
var array = [1, 2, 3, 5, 7];
var top = array.pop();
Run Code Online (Sandbox Code Playgroud)
问题是pop从数组中删除元素.要解决这个问题,我添加了另一行:
array.push(top);
Run Code Online (Sandbox Code Playgroud)
但这让我感到烦恼,我在这个项目中做了四到五次,直到现在.有没有更好的办法?
我有一个像这样的数组项列表:
const items = [
{ a: 1 },
{ b: 2 },
{ c: 3 },
]
Run Code Online (Sandbox Code Playgroud)
如何返回/记录最后一个元素:{ c: 3 }
到目前为止,这是我尝试过的:
let newarray = items.map((item) => {
console.log(item);
})
console.log(newarray);
Run Code Online (Sandbox Code Playgroud) 我有一个数组列表,并且我只想要数组列表中的最后一条记录,如何获取?
我想从该数组的最后一条记录中获取消息(即,嘿)。
这是我的代码:
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);
Run Code Online (Sandbox Code Playgroud)
请帮忙。提前致谢。