Quickly get the element at the specified position of the array in javascript

foreword

In JavaScript, if we need to obtain an element at a specified position in an array, usually, we generally use the following methods:

1. Get the specified element directly by subscript: arr[index], index is a non-negative number.

let arr = [1, 4, 5, 8, 10]
// 获取数组的第一个元素
let num1 = arr[0]
// 获取数组的最后一个元素
let num2 = arr[arr.length - 1]
// 获取数组的倒数第二个元素
let num3 = arr[arr.length - 2]
console.log(num1, num2, num3); // 1 10 8

It is more convenient to obtain the specified element according to the positive sequence position, but it is a bit cumbersome to obtain the specified element according to the reverse sequence position (the penultimate one) (need to calculate the positive sequence position). The index
is a positive integer or 0 in the positive sequence , and the index is a negative integer in the reverse sequence .

Then the expression for obtaining the corresponding position element can be expressed as:

index is a positive number or 0: arr[index]

index is negative: arr[arr.length + index]

2. Obtained by the slice method

let arr = [2, 4, 6, 8, 10]
// 获取数组的第一个元素
let num4 = arr.slice(0, 1)
// 获取数组的最后一个元素
let num5 = arr.slice(-1)
// 获取数组的倒数第二个元素
let num6 = arr.slice(-2, -1)
console.log(num4[0], num5[0], num6[0]); // 2 10 8

In fact, in other languages ​​such as python, if you want to get the penultimate element, you can get it directly through arr[index] (such as arr[-1] to get the penultimate element), but this is not supported in JavaScript of.

In order to more conveniently obtain the element at the specified position of the array (whether it is in positive or reverse order), the array provides a built-in method at() that can directly obtain the array element at the specified position through the element subscript

array's at() method

We use the at method to implement the above cases:

let arr2 = [2, 4, 6, 8, 10]
// 获取数组的第一个元素
let num4 = arr2.at(0)
// 获取数组的最后一个元素
let num5 = arr2.at(-1)
// 获取数组的倒数第二个元素
let num6 = arr2.at(-2)
console.log(num4, num5, num6); // 2 10 8

Syntax: at(index), index is an integer, including negative integers, indicating the number of elements from left to right.

Get an element that does not exist at the specified position and return undefined

let arr = [2, 4, 6, 8, 10]
console.log(arr[5]); // undefined

Get the element at the specified position of the class array

let likeArr = {
    
    
  length: 2,
  0: 'vue',
  1: 'react'
}
let lang = Array.prototype.at.call(likeArr, 0)
console.log(lang); // vue

Compared

method parameter range Ease of use
arr[index] non-negative integer Computation is required when obtaining elements in reverse order, which is relatively cumbersome
slice(startIndex, endIndex) integer Generally, one or two subscripts are required, which is relatively cumbersome
at(index) integer Only one subscript index is needed, which is relatively simple

Guess you like

Origin blog.csdn.net/weixin_46828094/article/details/129159211