选择 JavaScript 数组中的最后一个元素 [英] Selecting last element in JavaScript array

查看:38
本文介绍了选择 JavaScript 数组中的最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,可以实时更新用户的位置和路径,并将其显示在 Google 地图上.我的功能允许使用一个对象同时跟踪多个用户,该对象每秒更新一次.

I'm making an application that updates a user's location and path in real time and displays this on a Google Map. I have functionality that allows multiple users to be tracked at the same time using an object, which is updated every second.

现在,当用户按下 Android 应用中的按钮时,坐标会发送到数据库,每次位置发生变化时,地图上的标记都会更新(并形成多段线).

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).

因为我有多个用户,所以我发送了一个唯一且随机生成的字母数字字符串,以便我可以为每个用户显示一个单独的路径.当 JS 从数据库中提取这些数据时,它会检查用户是否存在,如果不存在,它会创建一个值为列表的新键.它看起来像这样:

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. 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)]}

我正在做的是将坐标列表存储为键的值,即用户的 ID.每次通过添加到列表来更改位置时,我的程序都会不断更新此列表(这可以正常工作).

What I'm doing is storing a list of coordinates as the value of the key, which is the user's 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.

每次位置更新时,我都会使用for (x in loc)"语句从列表中获取最后一个位置并使用它来更新标记.如何从哈希中的数组中选择最后一个元素?

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. How do I select this last element from the array within the hash?

推荐答案

如何访问数组的最后一个元素

看起来是这样的:

How to access last element of an array

It looks like that:

var my_array = /* some array here */;
var last_element = my_array[my_array.length - 1];

在你的情况下看起来像这样:

Which in your case looks like this:

var array1 = loc['f096012e-2497-485d-8adb-7ec0b9352c52'];
var last_element = array1[array1.length - 1];

或者,在更长的版本中,不创建新变量:

or, in longer version, without creating new variables:

loc['f096012e-2497-485d-8adb-7ec0b9352c52'][loc['f096012e-2497-485d-8adb-7ec0b9352c52'].length - 1];

如何添加方法使其更简单

如果您喜欢创建函数/快捷方式来完成此类任务,请使用以下代码:

How to add a method for getting it simpler

If you are a fan for creating functions/shortcuts to fulfill such tasks, the following code:

if (!Array.prototype.last){
    Array.prototype.last = function(){
        return this[this.length - 1];
    };
};

将允许您通过调用数组的 last() 方法来获取数组的最后一个元素,例如:

will allow you to get the last element of an array by invoking array's last() method, in your case eg.:

loc['f096012e-2497-485d-8adb-7ec0b9352c52'].last();

您可以在这里检查它是否有效:http://jsfiddle.net/D4NRN/

You can check that it works here: http://jsfiddle.net/D4NRN/

这篇关于选择 JavaScript 数组中的最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆