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

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

问题描述

我做的,在现实的时间和在谷歌地图显示这将更新用户的位置和路径的应用程序。我有功能,使多个用户在同一时间使用对象,这是每一个第二更新被跟踪。

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.

现在,当用户$ P $在Android应用pssed一个按钮,坐标发送到数据库中每一次的位置变化,一个标记在地图上更新(并形成折线)。

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在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?

推荐答案

它看起来像:

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];

如何添加得到它简单

的方法

如果您是创建函数/快捷方式完成这样的任务风扇,下面code:

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];
    };
};

将允许您通过调用数组最后一个()方法,你的情况如得到一个数组的最后一个元素:

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天全站免登陆