Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

javascript - 选择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?

(如何在哈希中从数组中选择最后一个元素?)

  ask by mkyong translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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

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

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

(你可以在这里查看它的工作原理: http//jsfiddle.net/D4NRN/)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...