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

JavaScript

Harry乐Harry

2020-03-12

我正在制作一个可实时更新用户的位置和路径并将其显示在Google Map上的应用程序。我具有允许使用一个对象(同时每秒更新一次)同时跟踪多个用户的功能。

现在,当用户按下Android应用程序中的按钮时,坐标将发送到数据库,并且每次位置更改时,都会在地图上更新标记(并形成折线)。

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

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。每次更改位置时,我的程序都会通过添加到列表中来不断更新此列表(这可以正常工作)。

我需要做的是每次位置更改时都更新标记的位置。我想通过选择数组中的最后一个项目来做到这一点,因为那将是最后一个已知的位置。现在,每次更改位置时,都会向地图添加一个新标记(示例中的每个点都会在该位置显示一个标记),以便继续添加标记。

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

第1073篇《选择JavaScript数组中的最后一个元素\[重复\]》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

7个回答
飞云神奇 2020.03.12

如果这对您的应用程序至关重要,请使用JavaScript对象。您不应该使用原始基元来管理应用程序的关键部分。由于这似乎是应用程序的核心,因此应改为使用对象。我在下面编写了一些代码来帮助您入门。该方法lastLocation将返回最后一个位置。


function User(id) {
    this.id = id;

    this.locations = [];

    this.getId = function() {
        return this.id;
    };

    this.addLocation = function(latitude, longitude) {
        this.locations[this.locations.length] = new google.maps.LatLng(latitude, longitude);
    };

    this.lastLocation = function() {
        return this.locations[this.locations.length - 1];
    };

    this.removeLastLocation = function() {
        return this.locations.pop();
    };

}

function Users() {
    this.users = {};

    this.generateId = function() {
        return Math.random();
    };

    this.createUser = function() {
        var id = this.generateId();
        this.users[id] = new User(id);
        return this.users[id];
    };

    this.getUser = function(id) {
        return this.users[id];
    };

    this.removeUser = function(id) {
        var user = this.getUser(id);
        delete this.users[id];

        return user;
    };

}


var users = new Users();

var user = users.createUser();

user.addLocation(0, 0);
user.addLocation(0, 1);
小宇宙西里 2020.03.12

这工作:

array.reverse()[0]
Stafan猿 2020.03.12
var arr = [1, 2, 3];
arr.slice(-1).pop(); // return 3 and arr = [1, 2, 3]

如果数组为空,则将返回undefined,并且不会更改数组的值。

Sam神乐番长 2020.03.12

使用slice()方法:

my_array.slice(-1)[0]
伽罗GreenNear 2020.03.12

您也可以.pop关闭最后一个元素。请注意,这将更改array的值,但是对您来说可能没问题。

var a = [1,2,3];
a.pop(); // 3
a // [1,2]
神乐十三 2020.03.12
var last = array.slice(-1)[0];

我发现-1处的slice对于获取最后一个元素(尤其是长度未知的数组)很有用,并且其性能比计算小于1的长度要好得多。

Mozilla Docs on Slice

选择最后一个数组元素的各种方法的性能

卡卡西乐 2020.03.12

将ES6解构数组与散布运算符一起使用

var last = [...yourArray].pop();

请注意,yourArray不会改变。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android