JavaScript lấy các phần tử đầu tiên của mảng

Phương thức shift() 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

shift()

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

Phương thức shift() 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 length là 0, thì hàm undefined được trả về

Phương thức pop() có hành vi tương tự như phương thức shift(), nhưng được áp dụng cho phần tử cuối cùng trong một mảng

Phương thức shift() là một phương thức độ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ột mảng mới đã loạ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 shift() là. 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 length và 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

Đ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

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

Phương thức shift() đọc thuộc tính length 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 là 0, thì length đượ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à undefined). 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 length được giảm đi một

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

Chúng ta có thể sử dụng phương thức slice() để lấy N phần tử đầu tiên từ một mảng. Ghi chú. Hàm slice trên mảng trả về một bản sao nông của mảng và không sửa đổi mảng ban đầu. Và nếu N lớn hơn kích thước của mảng thì nó sẽ không gặp bất kỳ lỗi nào và trả về toàn bộ mảng.

Làm cách nào để nhận 10 giá trị đầu tiên trong mảng JavaScript?

slice() method để lấy N phần tử đầu tiên của một mảng, e. g. const first3 = mảng. lát (0, 3). Mảng. slice() sẽ trả về một mảng mới chứa N phần tử đầu tiên của mảng ban đầu.

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

Sử dụng Luồng Java 8, .
to get first N elements from a list into a list, List firstNElementsList = list.stream().limit(n).collect(Collectors.toList());.
để lấy N phần tử đầu tiên từ một danh sách vào một Mảng, String[] firstNElementsArray = list. suối(). giới hạn(n). sưu tầm(Người sưu tập. liệt kê()). toArray(Chuỗi mới[n]);

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

Mảng JavaScript lát() . Phương thức slice() chọn từ một điểm bắt đầu nhất định, cho đến một điểm kết thúc (không bao gồm) đã cho. Phương thức slice() không thay đổi mảng ban đầu.