当前位置:网站首页>Summary of multiple methods for obtaining the last element of JS array
Summary of multiple methods for obtaining the last element of JS array
2022-06-23 20:44:00 【It workers】
I will discuss the text with you js There are many ways to get the last element of the array .
length
Because the array index is from 0 Start , So we can access the array length minus 1, This achieves the goal of accessing the last element .
notes : If the location of the index illegal , Then it will return to undefined.
var my_array = [1,2,3,4,5]; var last_element = my_array[my_array.length - 1]; // 5
prototype attribute
We can bind the last element of the access array to the prototype chain , This makes it easy to use for many times .
For example, the following code :
if (!Array.prototype.last){
Array.prototype.last = function(){
return this[this.length - 1];
};
};pop Method
because pop Is used to delete the last element of the array , And it returns the deleted element , So we can use this method directly .
For example, the following code :
var arr = [1,2,3,4,5]; var lastElement = [...arr].pop();//5
notes :pop The method has side effects , Will change the original array .
边栏推荐
- 【Golang】怎样优雅的清空切片
- 同花顺网上开户安全吗,佣金高不高
- UGeek大咖说 | 可观测之超融合存储系统的应用与设计
- [SAP ABAP] call API interface instance
- Rstudio 1.4 software installation package and installation tutorial
- OHOS LTS 3.0移植到RaspberryPi 4B
- Application of MySQL time function, simple problem
- Teach you how to develop desktop applications with web pages
- 35 year old crisis? It has become a synonym for programmers
- How to install SSL certificates in Microsoft Exchange 2010
猜你喜欢

Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181

Syntaxe des requêtes fédérées SQL (inline, left, right, full)

Implementation of microblog system based on SSM

Crise de 35 ans? Le volume intérieur est devenu synonyme de programmeur...

JS advanced programming version 4: generator learning

Tupu software digital twin intelligent water service, breaking through the development dilemma of sponge City

测试的重要性及目的

After the collapse of UST, will the stable currency market pattern usher in new opportunities?

LeetCode 260. Number III that appears only once

Syntax of SQL union query (inline, left, right, and full)
随机推荐
How PostgreSQL quickly locate blocking SQL
100 lines of code, using pyGame to make a snake game!
How to solve the problem of large traffic audio audit? What are the common approval methods?
Yaokui tower in Fengjie, Chongqing, after its completion, will be the safety tower for Sichuan river shipping with five local scholars in the company
The evolution of the "Rainbow Bridge" middleware platform for the acquisition database based on shardingsphere
【Golang】快速复习指南QuickReview(十)——goroutine池
Which is better for securities companies? I don't understand. Is it safe to open an account online?
Deep learning of handlebar handwriting (15): building your own corpus on hugging face
Crise de 35 ans? Le volume intérieur est devenu synonyme de programmeur...
【Golang】快速复习指南QuickReview(一)——字符串string
教你如何用网页开发桌面应用
Advantages of short video automatic audit? What are the difficulties of manual audit?
3000 frame animation illustrating why MySQL needs binlog, redo log and undo log
How do I view the server when I log in to the fortress machine? Operation guide for novice
How to solve the problem that the ID is not displayed when easycvr edits the national standard channel?
FPGA based electromagnetic ultrasonic pulse compression detection system paper + source file
Kinsoku Jikou Desu Sina stock interface change
Install bitgarden open source password manager
. Net cloud native architect training camp (rgca four step architecture method) -- learning notes
Want to open an account, is it safe to open an account on the stock Internet? Will the funds be cheated away?