

Using the new array.at() method
source link: http://www.js-craft.io/blog/using-the-new-array-at-method/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Using the new array.at() method
There is a new array method on the block. It's array.at()
and the main difference vs the normal square brackets access syntax is the support for the negative indexes.
Let's consider the following array:
const legumes = [
"beans",
"peanuts",
"clover",
"lentils"
]
At a first glace []
and at()
will do the same thing:
legumes[0] // "beans"
legumes.at(0) // "beans"
But while legumes[-1]
will end up in returning undefined
the legumes.at(-1)
will return "lentils"
.
legumes[-1] // undefined
legumes.at(-1) // "lentils"
With the classic square brackets syntax, we will need to use a trick like the following to access the last element in the array:
legumes[legumes.length - 1] // lentils
The negative indexes in the at() method will give us a more natural way to access elements at the end of the array:
legumes.at(-2) // "clover"
legumes.at(-3) // "peanuts"
Keep in mind that for the moment Safari does not yet support negative indexes.
I hope you have enjoyed this article and if you would like to get more articles about React and frontend development you can always sign up for my email list.
Newsletter subscribe:
Recommend
-
11
Alongside the plain object, the array is a widely used data structure in JavaScript. And a widely used operation on arrays is accessing elements by index. In this post, I’m going to present the new array method array.at(index)
-
17
How to Use Array Reduce Method in JavaScriptLet’s say you have an array of numbers: const numbers = [2, 4, 6]; How can you sum these numbers? Thanks to the array.reduce() method...
-
7
JavaScript array value of the method advertisements [].valueOf() method retuns array itself.According to this docu...
-
7
What is the effective method for adding an element to the array in java when the array size is small (not more than 5) advertisements The gene...
-
6
Using the every Method in an array Jul 9 ・1 min read ...
-
7
Hi Everyone 👋 Today I wanted to share some of the most common use cases for Array sort() method in JavaScript. The...
-
5
Method not found !!0[] System.Array.Empty()-黑暗執行緒 部署 ASP.NET MVC 程式遇到奇怪錯誤: Server Error in '/' Application. Method not found: '!!0[] System.Array.Empty()'. Description: An unhandled exce...
-
6
Best method to search for an array? advertisements I have an array (nodes[][]) that contains values of effecti...
-
8
ECMAScript 2022 introduces at() method in Array, String, TypedArray Feb 24, 2022 , by Chetan Gawai 2 minute read
-
3
If you need to split up a string into an array of substrings, then you can use the JavaScript split() method. In this article, I will go over the JavaScript split() method and provide code examples.
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK