Làm cách nào bạn có được phần tử đầu tiên trong một mảng trong JavaScript?

Phương thức

const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel
1 loại bỏ phần tử đầu tiên khỏi một mảng và trả về phần tử đã loại bỏ đó. Phương thức này thay đổi độ dài của mảng

Thử nó

cú pháp

shift[]

Giá trị trả về

Phần tử bị xóa khỏi mảng;

Sự miêu tả

Phương thức

const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel
1 loại bỏ phần tử tại chỉ mục thứ 0 và chuyển các giá trị tại các chỉ mục liên tiếp xuống dưới, sau đó trả về giá trị đã loại bỏ. Nếu thuộc tính
const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel
4 là 0, thì
const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel
2 được trả về

Phương thức

const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel
6 có hành vi tương tự như
const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel
1, nhưng được áp dụng cho phần tử cuối cùng trong một mảng

Phương pháp

const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel
1 là một phương pháp đột biến. Nó thay đổi độ dài và nội dung của
const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel
2. Trong trường hợp bạn muốn giá trị của
const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel
2 giống nhau, nhưng trả về mảng mới đã bỏ phần tử đầu tiên, bạn có thể sử dụng
const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel
4 để thay thế

Phương pháp

const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel
1 là chung chung. Nó chỉ mong đợi giá trị
const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel
2 có thuộc tính
const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel
4 và các thuộc tính có khóa số nguyên. Mặc dù các chuỗi cũng giống như mảng, nhưng phương pháp này không phù hợp để áp dụng cho chúng, vì các chuỗi là bất biến

ví dụ

Xóa một phần tử khỏi mảng

Đoạn mã sau hiển thị mảng

const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel
8 trước và sau khi loại bỏ phần tử đầu tiên của nó. Nó cũng hiển thị phần tử đã xóa

const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel

Sử dụng phương thức shift[] trong vòng lặp while

Phương thức shift[] thường được sử dụng trong điều kiện bên trong vòng lặp while. Trong ví dụ sau, mỗi lần lặp sẽ loại bỏ phần tử tiếp theo khỏi một mảng, cho đến khi nó trống

const names = ["Andrew", "Tyrone", "Paul", "Maria", "Gayatri"];

while [typeof [i = names.shift[]] !== "undefined"] {
  console.log[i];
}
// Andrew, Tyrone, Paul, Maria, Gayatri

Gọi shift[] trên các đối tượng không phải mảng

Phương thức

const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel
1 đọc thuộc tính
const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel
4 của
const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel
2. Nếu độ dài chuẩn hóa là 0, thì
const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel
4 được đặt lại thành
const names = ["Andrew", "Tyrone", "Paul", "Maria", "Gayatri"];

while [typeof [i = names.shift[]] !== "undefined"] {
  console.log[i];
}
// Andrew, Tyrone, Paul, Maria, Gayatri
3 [trong khi nó có thể là âm hoặc trước đó là
const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel
2]. Mặt khác, thuộc tính tại
const names = ["Andrew", "Tyrone", "Paul", "Maria", "Gayatri"];

while [typeof [i = names.shift[]] !== "undefined"] {
  console.log[i];
}
// Andrew, Tyrone, Paul, Maria, Gayatri
3 được trả lại và phần còn lại của thuộc tính được dịch chuyển sang trái một. Thuộc tính
const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log["myFish before:", JSON.stringify[myFish]];
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift[];

console.log["myFish after:", myFish];
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log["Removed this element:", shifted];
// Removed this element: angel
4 được giảm đi một

Làm cách nào để lấy phần tử đầu tiên của một mảng trong JavaScript?

Trong JavaScript, bạn chỉ cần làm. const first = arr[0];

Làm cách nào để lấy phần tử đầu tiên của tập hợp trong JavaScript?

Phương pháp 1. đầu tiên = đặt. giá trị[]. tiếp theo[]. giá trị .

Làm cách nào để lấy phần tử đầu tiên và cuối cùng của mảng trong JavaScript?

Mảng JavaScript không được lập chỉ mục. phần tử đầu tiên của mảng có chỉ số 0 , phần tử thứ hai có chỉ số 1 , v.v. — và phần tử cuối cùng có giá trị bằng thuộc tính độ dài của mảng trừ đi 1 .

Chỉ mục đầu tiên của một mảng trong JavaScript là gì?

Ghi chú. Trong một mảng, phần tử đầu tiên có chỉ số [vị trí] 0 , phần tử thứ hai có chỉ số 1,.

Chủ Đề