选择JavaScript数组中的最后一个元素[重复]

本文翻译自:Selecting last element in JavaScript array [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

I'm making an application that updates a user's location and path in real time and displays this on a Google Map. 我正在制作一个实时更新用户位置和路径的应用程序,并在Google地图上显示。 I have functionality that allows multiple users to be tracked at the same time using an object, which is updated every second. 我的功能允许使用每秒更新一次的对象同时跟踪多个用户。

Right now, when a user pressed a button in the Android app, the coordinates are sent to a database and each time the location changes, a marker is updated on the map (and a polyline is formed). 现在,当用户按下Android应用程序中的按钮时,坐标将被发送到数据库,每次位置更改时,都会在地图上更新标记(并形成折线)。

Since I have multiple users, I send a unique and randomly generated alphanumeric string so that I can display an individual path for each user. 由于我有多个用户,因此我会发送一个唯一且随机生成的字母数字字符串,以便为每个用户显示单独的路径。 When the JS pulls this data from the database, it checks if the user exists, if it does not, it creates a new key with the value being a list. 当JS从数据库中提取此数据时,它会检查用户是否存在,如果不存在,则会创建一个值为列表的新密钥。 It would look something like this: 它看起来像这样:

loc = {f096012e-2497-485d-8adb-7ec0b9352c52: [new google.maps.LatLng(39, -86),
                                              new google.maps.LatLng(38, -87),
                                              new google.maps.LatLng(37, -88)],
       44ed0662-1a9e-4c0e-9920-106258dcc3e7: [new google.maps.LatLng(40, -83),
                                              new google.maps.LatLng(41, -82),
                                              new google.maps.LatLng(42, -81)]}

What I'm doing is storing a list of coordinates as the value of the key, which is the user's ID. 我正在做的是存储一个坐标列表作为键的值,这是用户的ID。 My program keeps updating this list each time the location is changed by adding to the list (this works properly). 每次更改位置时,我的程序会通过添加到列表来更新此列表(这可以正常工作)。

What I need to do is update the marker's location each time the location changes. 我需要做的是每次位置更改时更新标记的位置。 I would like to do this by selecting the last item in the array since that would be the last known location. 我想通过选择数组中的最后一项来完成此操作,因为这将是最后一个已知位置。 Right now, each time the location is changed a new marker is added to the map (each one of the points in the example would show a marker at that location) so markers continue to be added. 现在,每次更改位置时,都会向地图添加一个新标记(示例中的每个点都会在该位置显示标记),因此继续添加标记。

I would use a ´for (x in loc)` statement each time the location updates to grab the last location from the list and use that to update the marker. 每次位置更新时,我会使用'for(x in loc)`语句从列表中获取最后一个位置并使用它来更新标记。 How do I select this last element from the array within the hash? 如何在哈希中从数组中选择最后一个元素?


#1楼

参考:https://stackoom.com/question/byPJ/选择JavaScript数组中的最后一个元素-重复


#2楼

使用slice()方法:

my_array.slice(-1)[0]

#3楼

You can also .pop off the last element. 你也可以.pop关闭最后一个元素。 Be careful, this will change the value of the array , but that might be OK for you. 小心,这会改变数组的值 ,但这对你来说可能没问题。

var a = [1,2,3];
a.pop(); // 3
a // [1,2]

#4楼

var last = array.slice(-1)[0];

I find slice at -1 useful for getting the last element (especially of an array of unknown length) and the performance is much better than calculating the length less 1. 我发现-1处的切片对于获取最后一个元素(特别是未知长度的数组)非常有用,并且性能比计算长度减去1要好得多。

Mozilla Docs on Slice 片上的Mozilla文档

Performance of the various methods for selecting last array element 用于选择最后一个数组元素的各种方法的性能


#5楼

var arr = [1, 2, 3];
arr.slice(-1).pop(); // return 3 and arr = [1, 2, 3]

This will return undefined if the array is empty and this will not change the value of the array. 如果数组为空,这将返回undefined,这不会更改数组的值。


#6楼

You can define a getter on Array.prototype : 您可以在Array.prototype上定义一个getter

 if (!Array.prototype.hasOwnProperty("last")) { Object.defineProperty(Array.prototype, "last", { get() { return this[this.length - 1]; } }); } console.log([9, 8, 7, 6].last); // => 6 

As you can see, access doesn't look like a function call; 如您所见,访问看起来不像函数调用; the getter function is called internally. getter函数在内部调用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值