Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js array最后一个元素

作者:此甥孓狠善良   发布日期:2025-02-13   浏览:94

// 获取数组的最后一个元素

// 方法1: 使用 array[array.length - 1]
const arr = [1, 2, 3, 4, 5];
const lastElement1 = arr[arr.length - 1];
console.log(lastElement1); // 输出: 5
// 解释: 通过访问数组长度减1的索引位置,可以获取到数组的最后一个元素。

// 方法2: 使用 Array.prototype.at()
const lastElement2 = arr.at(-1);
console.log(lastElement2); // 输出: 5
// 解释: at() 方法接受一个整数参数,表示要访问的元素的位置。负数表示从数组末尾开始计算。

上一篇:js foreach是异步吗

下一篇:js 页面重新加载

大家都在看

js 数组对象排序

js 数组删掉第一个值

js fill

js 数组复制

js 复制数组

js 数组拷贝

js 对象转数组

js 深拷贝数组

js 获取今天年月日

js jsonp

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站