Lấy phần tử cuối cùng thứ 2 của mảng javascript

Phương thức

const numbers = [2, 5, 9, 2];
numbers.lastIndexOf[2]; // 3
numbers.lastIndexOf[7]; // -1
numbers.lastIndexOf[2, 3]; // 3
numbers.lastIndexOf[2, 2]; // 0
numbers.lastIndexOf[2, -2]; // 0
numbers.lastIndexOf[2, -1]; // 3
9 trả về chỉ mục cuối cùng mà tại đó có thể tìm thấy phần tử đã cho trong mảng hoặc -1 nếu không có phần tử đó. Mảng được tìm kiếm ngược, bắt đầu từ
const numbers = [2, 5, 9, 2];
numbers.lastIndexOf[2]; // 3
numbers.lastIndexOf[7]; // -1
numbers.lastIndexOf[2, 3]; // 3
numbers.lastIndexOf[2, 2]; // 0
numbers.lastIndexOf[2, -2]; // 0
numbers.lastIndexOf[2, -1]; // 3
0

lastIndexOf[searchElement]
lastIndexOf[searchElement, fromIndex]

const numbers = [2, 5, 9, 2];
numbers.lastIndexOf[2]; // 3
numbers.lastIndexOf[7]; // -1
numbers.lastIndexOf[2, 3]; // 3
numbers.lastIndexOf[2, 2]; // 0
numbers.lastIndexOf[2, -2]; // 0
numbers.lastIndexOf[2, -1]; // 3
1

Phần tử cần xác định vị trí trong mảng

const numbers = [2, 5, 9, 2];
numbers.lastIndexOf[2]; // 3
numbers.lastIndexOf[7]; // -1
numbers.lastIndexOf[2, 3]; // 3
numbers.lastIndexOf[2, 2]; // 0
numbers.lastIndexOf[2, -2]; // 0
numbers.lastIndexOf[2, -1]; // 3
0 Tùy chọn

Chỉ mục dựa trên số không để bắt đầu tìm kiếm ngược,

  • Chỉ số âm đếm ngược từ cuối mảng — nếu sử dụng
    const numbers = [2, 5, 9, 2];
    numbers.lastIndexOf[2]; // 3
    numbers.lastIndexOf[7]; // -1
    numbers.lastIndexOf[2, 3]; // 3
    numbers.lastIndexOf[2, 2]; // 0
    numbers.lastIndexOf[2, -2]; // 0
    numbers.lastIndexOf[2, -1]; // 3
    
    3,
    const numbers = [2, 5, 9, 2];
    numbers.lastIndexOf[2]; // 3
    numbers.lastIndexOf[7]; // -1
    numbers.lastIndexOf[2, 3]; // 3
    numbers.lastIndexOf[2, 2]; // 0
    numbers.lastIndexOf[2, -2]; // 0
    numbers.lastIndexOf[2, -1]; // 3
    
    0
  • Nếu
    const numbers = [2, 5, 9, 2];
    numbers.lastIndexOf[2]; // 3
    numbers.lastIndexOf[7]; // -1
    numbers.lastIndexOf[2, 3]; // 3
    numbers.lastIndexOf[2, 2]; // 0
    numbers.lastIndexOf[2, -2]; // 0
    numbers.lastIndexOf[2, -1]; // 3
    
    1, mảng không được tìm kiếm và trả về
    const numbers = [2, 5, 9, 2];
    numbers.lastIndexOf[2]; // 3
    numbers.lastIndexOf[7]; // -1
    numbers.lastIndexOf[2, 3]; // 3
    numbers.lastIndexOf[2, 2]; // 0
    numbers.lastIndexOf[2, -2]; // 0
    numbers.lastIndexOf[2, -1]; // 3
    
    2. Bạn có thể nghĩ về nó một cách khái niệm như bắt đầu từ một vị trí không tồn tại trước khi bắt đầu mảng và đi ngược lại từ đó. Không có phần tử mảng nào trên đường đi, vì vậy không bao giờ tìm thấy
    const numbers = [2, 5, 9, 2];
    numbers.lastIndexOf[2]; // 3
    numbers.lastIndexOf[7]; // -1
    numbers.lastIndexOf[2, 3]; // 3
    numbers.lastIndexOf[2, 2]; // 0
    numbers.lastIndexOf[2, -2]; // 0
    numbers.lastIndexOf[2, -1]; // 3
    
    1
  • Nếu
    const numbers = [2, 5, 9, 2];
    numbers.lastIndexOf[2]; // 3
    numbers.lastIndexOf[7]; // -1
    numbers.lastIndexOf[2, 3]; // 3
    numbers.lastIndexOf[2, 2]; // 0
    numbers.lastIndexOf[2, -2]; // 0
    numbers.lastIndexOf[2, -1]; // 3
    
    4 hoặc
    const numbers = [2, 5, 9, 2];
    numbers.lastIndexOf[2]; // 3
    numbers.lastIndexOf[7]; // -1
    numbers.lastIndexOf[2, 3]; // 3
    numbers.lastIndexOf[2, 2]; // 0
    numbers.lastIndexOf[2, -2]; // 0
    numbers.lastIndexOf[2, -1]; // 3
    
    0 bị bỏ qua, thì
    const numbers = [2, 5, 9, 2];
    numbers.lastIndexOf[2]; // 3
    numbers.lastIndexOf[7]; // -1
    numbers.lastIndexOf[2, 3]; // 3
    numbers.lastIndexOf[2, 2]; // 0
    numbers.lastIndexOf[2, -2]; // 0
    numbers.lastIndexOf[2, -1]; // 3
    
    6 được sử dụng, khiến toàn bộ mảng được tìm kiếm. Bạn có thể nghĩ về nó một cách khái niệm như bắt đầu từ một vị trí không tồn tại bên ngoài phần cuối của mảng và đi ngược lại từ đó. Cuối cùng, nó đến vị trí cuối thực của mảng, tại thời điểm đó, nó bắt đầu tìm kiếm ngược thông qua các phần tử mảng thực

Chỉ số cuối cùng của phần tử trong mảng;

Phương thức

const numbers = [2, 5, 9, 2];
numbers.lastIndexOf[2]; // 3
numbers.lastIndexOf[7]; // -1
numbers.lastIndexOf[2, 3]; // 3
numbers.lastIndexOf[2, 2]; // 0
numbers.lastIndexOf[2, -2]; // 0
numbers.lastIndexOf[2, -1]; // 3
9 so sánh
const numbers = [2, 5, 9, 2];
numbers.lastIndexOf[2]; // 3
numbers.lastIndexOf[7]; // -1
numbers.lastIndexOf[2, 3]; // 3
numbers.lastIndexOf[2, 2]; // 0
numbers.lastIndexOf[2, -2]; // 0
numbers.lastIndexOf[2, -1]; // 3
1 với các phần tử của mảng bằng cách sử dụng đẳng thức nghiêm ngặt [cùng một thuật toán được sử dụng bởi toán tử
const numbers = [2, 5, 9, 2];
numbers.lastIndexOf[2]; // 3
numbers.lastIndexOf[7]; // -1
numbers.lastIndexOf[2, 3]; // 3
numbers.lastIndexOf[2, 2]; // 0
numbers.lastIndexOf[2, -2]; // 0
numbers.lastIndexOf[2, -1]; // 3
9]

Phương pháp

const numbers = [2, 5, 9, 2];
numbers.lastIndexOf[2]; // 3
numbers.lastIndexOf[7]; // -1
numbers.lastIndexOf[2, 3]; // 3
numbers.lastIndexOf[2, 2]; // 0
numbers.lastIndexOf[2, -2]; // 0
numbers.lastIndexOf[2, -1]; // 3
9 bỏ qua các ô trống trong

Phương pháp

const numbers = [2, 5, 9, 2];
numbers.lastIndexOf[2]; // 3
numbers.lastIndexOf[7]; // -1
numbers.lastIndexOf[2, 3]; // 3
numbers.lastIndexOf[2, 2]; // 0
numbers.lastIndexOf[2, -2]; // 0
numbers.lastIndexOf[2, -1]; // 3
9 là. Nó chỉ mong đợi giá trị
const indices = [];
const array = ["a", "b", "a", "c", "a", "d"];
const element = "a";
let idx = array.lastIndexOf[element];
while [idx !== -1] {
  indices.push[idx];
  idx = idx > 0 ? array.lastIndexOf[element, idx - 1] : -1;
}

console.log[indices];
// [4, 2, 0]
2 có thuộc tính
const indices = [];
const array = ["a", "b", "a", "c", "a", "d"];
const element = "a";
let idx = array.lastIndexOf[element];
while [idx !== -1] {
  indices.push[idx];
  idx = idx > 0 ? array.lastIndexOf[element, idx - 1] : -1;
}

console.log[indices];
// [4, 2, 0]
3 và các thuộc tính có khóa số nguyên

Ví dụ sau sử dụng

const indices = [];
const array = ["a", "b", "a", "c", "a", "d"];
const element = "a";
let idx = array.lastIndexOf[element];
while [idx !== -1] {
  indices.push[idx];
  idx = idx > 0 ? array.lastIndexOf[element, idx - 1] : -1;
}

console.log[indices];
// [4, 2, 0]
4 để định vị các giá trị trong một mảng

const numbers = [2, 5, 9, 2];
numbers.lastIndexOf[2]; // 3
numbers.lastIndexOf[7]; // -1
numbers.lastIndexOf[2, 3]; // 3
numbers.lastIndexOf[2, 2]; // 0
numbers.lastIndexOf[2, -2]; // 0
numbers.lastIndexOf[2, -1]; // 3

Ví dụ sau sử dụng

const indices = [];
const array = ["a", "b", "a", "c", "a", "d"];
const element = "a";
let idx = array.lastIndexOf[element];
while [idx !== -1] {
  indices.push[idx];
  idx = idx > 0 ? array.lastIndexOf[element, idx - 1] : -1;
}

console.log[indices];
// [4, 2, 0]
4 để tìm tất cả các chỉ số của một phần tử trong một mảng nhất định, sử dụng
const indices = [];
const array = ["a", "b", "a", "c", "a", "d"];
const element = "a";
let idx = array.lastIndexOf[element];
while [idx !== -1] {
  indices.push[idx];
  idx = idx > 0 ? array.lastIndexOf[element, idx - 1] : -1;
}

console.log[indices];
// [4, 2, 0]
6 để thêm chúng vào một mảng khác khi chúng được tìm thấy

const indices = [];
const array = ["a", "b", "a", "c", "a", "d"];
const element = "a";
let idx = array.lastIndexOf[element];
while [idx !== -1] {
  indices.push[idx];
  idx = idx > 0 ? array.lastIndexOf[element, idx - 1] : -1;
}

console.log[indices];
// [4, 2, 0]

Lưu ý rằng chúng ta phải xử lý riêng trường hợp

const indices = [];
const array = ["a", "b", "a", "c", "a", "d"];
const element = "a";
let idx = array.lastIndexOf[element];
while [idx !== -1] {
  indices.push[idx];
  idx = idx > 0 ? array.lastIndexOf[element, idx - 1] : -1;
}

console.log[indices];
// [4, 2, 0]
7 ở đây vì phần tử sẽ luôn được tìm thấy bất kể tham số
const numbers = [2, 5, 9, 2];
numbers.lastIndexOf[2]; // 3
numbers.lastIndexOf[7]; // -1
numbers.lastIndexOf[2, 3]; // 3
numbers.lastIndexOf[2, 2]; // 0
numbers.lastIndexOf[2, -2]; // 0
numbers.lastIndexOf[2, -1]; // 3
0 nếu nó là phần tử đầu tiên của mảng. Điều này khác với phương pháp
const indices = [];
const array = ["a", "b", "a", "c", "a", "d"];
const element = "a";
let idx = array.lastIndexOf[element];
while [idx !== -1] {
  indices.push[idx];
  idx = idx > 0 ? array.lastIndexOf[element, idx - 1] : -1;
}

console.log[indices];
// [4, 2, 0]
9

Bạn không thể sử dụng

const numbers = [2, 5, 9, 2];
numbers.lastIndexOf[2]; // 3
numbers.lastIndexOf[7]; // -1
numbers.lastIndexOf[2, 3]; // 3
numbers.lastIndexOf[2, 2]; // 0
numbers.lastIndexOf[2, -2]; // 0
numbers.lastIndexOf[2, -1]; // 3
9 để tìm kiếm các vị trí trống trong các mảng thưa thớt

const numbers = [2, 5, 9, 2];
numbers.lastIndexOf[2]; // 3
numbers.lastIndexOf[7]; // -1
numbers.lastIndexOf[2, 3]; // 3
numbers.lastIndexOf[2, 2]; // 0
numbers.lastIndexOf[2, -2]; // 0
numbers.lastIndexOf[2, -1]; // 3
6

Phương thức

const numbers = [2, 5, 9, 2];
numbers.lastIndexOf[2]; // 3
numbers.lastIndexOf[7]; // -1
numbers.lastIndexOf[2, 3]; // 3
numbers.lastIndexOf[2, 2]; // 0
numbers.lastIndexOf[2, -2]; // 0
numbers.lastIndexOf[2, -1]; // 3
9 đọc thuộc tính
const indices = [];
const array = ["a", "b", "a", "c", "a", "d"];
const element = "a";
let idx = array.lastIndexOf[element];
while [idx !== -1] {
  indices.push[idx];
  idx = idx > 0 ? array.lastIndexOf[element, idx - 1] : -1;
}

console.log[indices];
// [4, 2, 0]
3 của
const indices = [];
const array = ["a", "b", "a", "c", "a", "d"];
const element = "a";
let idx = array.lastIndexOf[element];
while [idx !== -1] {
  indices.push[idx];
  idx = idx > 0 ? array.lastIndexOf[element, idx - 1] : -1;
}

console.log[indices];
// [4, 2, 0]
2 và sau đó truy cập từng chỉ mục số nguyên

Làm cách nào để lấy phần tử cuối cùng thứ 2 trong một mảng JavaScript?

Để lấy phần tử thứ hai đến phần tử cuối cùng trong một mảng, gọi phương thức at[] trên mảng, truyền vào đó -2 làm tham số, e. g. mảng. tại[-2] . Phương thức at trả về phần tử mảng tại chỉ mục đã chỉ định.

Bạn sẽ truy cập phần tử thứ hai trong một mảng * như thế nào?

Để truy cập các phần tử của một mảng tại một vị trí nhất định [được gọi là vị trí chỉ mục], hãy sử dụng hàm element_at[] và chỉ định tên mảng cũng như vị trí chỉ mục: If the index is greater than 0, element_at[] returns the element that you specify, counting from the beginning to the end of the array.

Làm cách nào để tìm phần tử cuối cùng trong mảng JavaScript?

Bản trình diễn JavaScript. Mảng. Nếu bạn cần tìm. phần tử đầu tiên phù hợp, sử dụng find[]. chỉ mục của phần tử phù hợp cuối cùng trong mảng, sử dụng findLastIndex[] . chỉ số của một giá trị, sử dụng indexOf[].

Làm cách nào để lấy chỉ mục cuối cùng thứ hai của chuỗi trong JavaScript?

Để lấy hai ký tự cuối cùng của một chuỗi trong JavaScript, gọi phương thức slice[] trên chuỗi, chuyển -2 làm đối số. For example, str. slice[-2] returns a new string containing the last two characters of str .

Chủ Đề