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óaconst 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