Bạn thường sẽ cần xóa một phần tử khỏi một mảng trong JavaScript, cho dù đó là cấu trúc dữ liệu hàng đợi hay có thể từ Trạng thái phản ứng của bạn
Trong nửa đầu của bài viết này, bạn sẽ tìm hiểu tất cả các phương pháp cho phép bạn xóa một phần tử khỏi mảng mà không làm thay đổi mảng ban đầu. Trên thực tế, đây là điều bạn sẽ muốn làm thường xuyên nhất. Ví dụ: nếu bạn không muốn thay đổi Trạng thái phản ứng của mình. Hoặc mảng được sử dụng trong các phần khác của mã của bạn và việc thay đổi nó sẽ gây ra sự cố không mong muốn
Luôn luôn tốt hơn để tránh đột biến
Tuy nhiên, để đầy đủ, nửa sau của bài viết sẽ liệt kê các phương pháp để xóa một phần tử khỏi mảng tại chỗ. Các phương thức này tự làm thay đổi mảng
Tại đây, bạn có thể tìm thấy một bản tóm tắt hữu ích về nội dung bài viết, nếu bạn muốn điều hướng đến một phần cụ thể
Cách xóa phần tử khỏi mảng mà không làm thay đổi mảng
Nếu bạn có một mảng đầu vào, chẳng hạn như một tham số chức năng, thì các phương pháp hay nhất là bạn không nên thay đổi mảng đó. Thay vào đó bạn nên tạo một cái mới
Có một số phương pháp bạn có thể sử dụng để xóa một mục cụ thể khỏi mảng mà không làm thay đổi mảng
Để tránh làm thay đổi mảng, một mảng mới sẽ được tạo mà không có phần tử bạn muốn loại bỏ
Bạn có thể sử dụng các phương pháp như
9const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f']; const copyWithoutLastElement = arrayOfLetters.slice[0, -1]; // arrayOfLetters is unchanged console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f'] console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
9 cùng vớiconst arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f']; const copyWithoutLastElement = arrayOfLetters.slice[0, -1]; // arrayOfLetters is unchanged console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f'] console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
1const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f']; const halfBeforeTheUnwantedElement = arrayOfLetters.slice[0, 2] const halfAfterTheUnwantedElement = arrayOfLetters[3]; const copyWithoutThirdElement = halfBeforeTheUnwantedElement.concat[halfAfterTheUnwantedElement]; // arrayOfLetters is unchanged console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f'] console.log[copyWithoutFifthElement] // ['a', 'b', 'd', 'e', 'f']
2const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f']; const halfBeforeTheUnwantedElement = arrayOfLetters.slice[0, 2] const halfAfterTheUnwantedElement = arrayOfLetters[3]; const copyWithoutThirdElement = halfBeforeTheUnwantedElement.concat[halfAfterTheUnwantedElement]; // arrayOfLetters is unchanged console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f'] console.log[copyWithoutFifthElement] // ['a', 'b', 'd', 'e', 'f']
- Một vòng lặp
4 vàconst arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f']; const copyWithoutLastElement = arrayOfLetters.slice[0, -1]; // arrayOfLetters is unchanged console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f'] console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
4const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f']; const halfBeforeTheUnwantedElement = arrayOfLetters.slice[0, 2] const halfAfterTheUnwantedElement = arrayOfLetters[3]; const copyWithoutThirdElement = halfBeforeTheUnwantedElement.concat[halfAfterTheUnwantedElement]; // arrayOfLetters is unchanged console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f'] console.log[copyWithoutFifthElement] // ['a', 'b', 'd', 'e', 'f']
Hãy xem chi tiết cách bạn có thể sử dụng từng phần tử này để xóa phần tử khỏi mảng mà không làm thay đổi phần tử ban đầu
Loại bỏ phần tử đầu tiên của một mảng bằng slice
Nếu bạn muốn xóa phần tử đầu tiên trong một mảng, bạn có thể sử dụng
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
9 trên một mảng có tên const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const halfBeforeTheUnwantedElement = arrayOfLetters.slice[0, 2]
const halfAfterTheUnwantedElement = arrayOfLetters[3];
const copyWithoutThirdElement = halfBeforeTheUnwantedElement.concat[halfAfterTheUnwantedElement];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutFifthElement] // ['a', 'b', 'd', 'e', 'f']
7 như thế này. const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const halfBeforeTheUnwantedElement = arrayOfLetters.slice[0, 2]
const halfAfterTheUnwantedElement = arrayOfLetters[3];
const copyWithoutThirdElement = halfBeforeTheUnwantedElement.concat[halfAfterTheUnwantedElement];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutFifthElement] // ['a', 'b', 'd', 'e', 'f']
8Đây là một ví dụ hoàn chỉnh, trong đó bạn muốn xóa phần tử đầu tiên khỏi một mảng chứa 6 chữ cái đầu tiên của bảng chữ cái
// the starting array
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
// here the array is copied, without the first element
const copyWithoutFirstElement = arrayOfLetters.slice[1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
// and copyWithoutFirstElement contains the letters from b to f
console.log[copyWithoutFirstElement] // ['b', 'c', 'd', 'e', 'f']
Phương thức slice
có thể lấy một số làm đối số và trong trường hợp này, nó sao chép từ chỉ mục đó đến cuối mảng. Vì vậy, sử dụng
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const arrayWithoutD = arrayOfLetters.filter[function [letter] {
return letter !== 'd';
}];
// arrayOfLetters is unchanged
console.log[arrayOfLetters]; // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[arrayWithoutD]; // ['a', 'b', 'c', 'e', 'f']
0 sẽ tạo một bản sao của mảng const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const arrayWithoutD = arrayOfLetters.filter[function [letter] {
return letter !== 'd';
}];
// arrayOfLetters is unchanged
console.log[arrayOfLetters]; // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[arrayWithoutD]; // ['a', 'b', 'c', 'e', 'f']
1 loại trừ phần tử đầu tiênXóa phần tử cuối cùng của một mảng bằng slice
Nếu phần tử bạn muốn loại bỏ là phần tử cuối cùng của mảng, bạn có thể sử dụng
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
9 trên mảng có tên const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const halfBeforeTheUnwantedElement = arrayOfLetters.slice[0, 2]
const halfAfterTheUnwantedElement = arrayOfLetters[3];
const copyWithoutThirdElement = halfBeforeTheUnwantedElement.concat[halfAfterTheUnwantedElement];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutFifthElement] // ['a', 'b', 'd', 'e', 'f']
7 theo cách này. const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const arrayWithoutD = arrayOfLetters.filter[function [letter] {
return letter !== 'd';
}];
// arrayOfLetters is unchanged
console.log[arrayOfLetters]; // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[arrayWithoutD]; // ['a', 'b', 'c', 'e', 'f']
5Dưới đây là một ví dụ hoàn chỉnh sử dụng cùng một mảng bảng chữ cái ở trên, bắt đầu bằng một mảng gồm 6 chữ cái đầu tiên trong bảng chữ cái
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
Phương thức slice
có tới hai tham số. Chỉ mục đầu tiên của slice
cho biết chỉ mục nào sẽ bắt đầu sao chép và đối số thứ hai cho biết phần tử nào sẽ được sao chép – nhưng nó không bao gồm
slice
chấp nhận chỉ số âm để tính từ cuối. Điều này có nghĩa là viết
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const arrayWithoutD = arrayOfLetters.filter[function [letter] {
return letter !== 'd';
}];
// arrayOfLetters is unchanged
console.log[arrayOfLetters]; // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[arrayWithoutD]; // ['a', 'b', 'c', 'e', 'f']
9 có nghĩa là chỉ số cuối cùng. Vì vậy, từ const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const arrayWithoutB = [];
for [let i = 0; i < arrayOfLetters.length; i++] {
if [arrayOfLetters[i] !== 'b'] {
arrayWithoutH.push[arrayOfLetters[i]];
}
}
// arrayOfLetters is unchanged
console.log[arrayOfLetters]; // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[arrayWithoutB]; // ['a', 'c', 'd', 'e', 'f']
0 đến const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const arrayWithoutD = arrayOfLetters.filter[function [letter] {
return letter !== 'd';
}];
// arrayOfLetters is unchanged
console.log[arrayOfLetters]; // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[arrayWithoutD]; // ['a', 'b', 'c', 'e', 'f']
9 có nghĩa là tạo một bản sao từ chỉ mục const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const arrayWithoutB = [];
for [let i = 0; i < arrayOfLetters.length; i++] {
if [arrayOfLetters[i] !== 'b'] {
arrayWithoutH.push[arrayOfLetters[i]];
}
}
// arrayOfLetters is unchanged
console.log[arrayOfLetters]; // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[arrayWithoutB]; // ['a', 'c', 'd', 'e', 'f']
0 cho đến [nhưng không bao gồm] chỉ mục cuối cùng. Kết quả cuối cùng là phần tử cuối cùng không được bao gồm trong bản saoLoại bỏ một phần tử tại bất kỳ vị trí nào của một mảng với slice
và const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
2
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
Nếu bạn muốn tạo một bản sao thiếu một thành phần ở bất kỳ chỉ mục nào, bạn có thể sử dụng đồng thời
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const arrayWithoutB = [];
for [let i = 0; i < arrayOfLetters.length; i++] {
if [arrayOfLetters[i] !== 'b'] {
arrayWithoutH.push[arrayOfLetters[i]];
}
}
// arrayOfLetters is unchanged
console.log[arrayOfLetters]; // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[arrayWithoutB]; // ['a', 'c', 'd', 'e', 'f']
5 và const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const arrayWithoutB = [];
for [let i = 0; i < arrayOfLetters.length; i++] {
if [arrayOfLetters[i] !== 'b'] {
arrayWithoutH.push[arrayOfLetters[i]];
}
}
// arrayOfLetters is unchanged
console.log[arrayOfLetters]; // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[arrayWithoutB]; // ['a', 'c', 'd', 'e', 'f']
6 theo cách này. const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const arrayWithoutB = [];
for [let i = 0; i < arrayOfLetters.length; i++] {
if [arrayOfLetters[i] !== 'b'] {
arrayWithoutH.push[arrayOfLetters[i]];
}
}
// arrayOfLetters is unchanged
console.log[arrayOfLetters]; // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[arrayWithoutB]; // ['a', 'c', 'd', 'e', 'f']
7 trong đó const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const arrayWithoutB = [];
for [let i = 0; i < arrayOfLetters.length; i++] {
if [arrayOfLetters[i] !== 'b'] {
arrayWithoutH.push[arrayOfLetters[i]];
}
}
// arrayOfLetters is unchanged
console.log[arrayOfLetters]; // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[arrayWithoutB]; // ['a', 'c', 'd', 'e', 'f']
8 là chỉ mục của phần tử bạn muốn xóaconst arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const halfBeforeTheUnwantedElement = arrayOfLetters.slice[0, 2]
const halfAfterTheUnwantedElement = arrayOfLetters[3];
const copyWithoutThirdElement = halfBeforeTheUnwantedElement.concat[halfAfterTheUnwantedElement];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutFifthElement] // ['a', 'b', 'd', 'e', 'f']
Việc sử dụng slice
này là một cách để kết hợp hai cách sử dụng trước đó
Lần sử dụng đầu tiên của slice
sẽ tạo một mảng từ đầu đến ngay trước phần tử bạn muốn xóa
Cách sử dụng thứ hai của slice
tạo một mảng từ sau phần tử bạn muốn xóa đến cuối mảng
Hai mảng được nối với nhau bằng
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
2 để tạo thành một mảng tương tự như mảng bắt đầu, nhưng không có phần tử cụ thểXóa một phần tử có giá trị nhất định bằng const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
3
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
Nếu bạn muốn xóa một phần tử có giá trị nhất định, bạn có thể sử dụng
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const halfBeforeTheUnwantedElement = arrayOfLetters.slice[0, 2]
const halfAfterTheUnwantedElement = arrayOfLetters[3];
const copyWithoutThirdElement = halfBeforeTheUnwantedElement.concat[halfAfterTheUnwantedElement];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutFifthElement] // ['a', 'b', 'd', 'e', 'f']
2. Hãy lấy cùng một const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const arrayWithoutD = arrayOfLetters.filter[function [letter] {
return letter !== 'd';
}];
// arrayOfLetters is unchanged
console.log[arrayOfLetters]; // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[arrayWithoutD]; // ['a', 'b', 'c', 'e', 'f']
1 và tạo một bản sao không có const arrayOfFruits = ['olive', 'apricot', 'cherry', 'peach', 'plum', 'mango'];
const [ , ...arrayOfCulinaryFruits] = arrayOfFruits;
// arrayOfFruits is unchanged
console.log[arrayOfFruits]; // ['olive', 'apricot', 'cherry', 'peach', 'plum', 'mango']
console.log[arrayOfCulinaryFruits]; // ['apricot', 'cherry', 'peach', 'plum', 'mango']
6const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const arrayWithoutD = arrayOfLetters.filter[function [letter] {
return letter !== 'd';
}];
// arrayOfLetters is unchanged
console.log[arrayOfLetters]; // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[arrayWithoutD]; // ['a', 'b', 'c', 'e', 'f']
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
3 nhận một cuộc gọi lại và kiểm tra tất cả các phần tử của mảng với cuộc gọi lại đó. Nó giữ các phần tử mà hàm gọi lại trả về const arrayOfFruits = ['olive', 'apricot', 'cherry', 'peach', 'plum', 'mango'];
const [ , ...arrayOfCulinaryFruits] = arrayOfFruits;
// arrayOfFruits is unchanged
console.log[arrayOfFruits]; // ['olive', 'apricot', 'cherry', 'peach', 'plum', 'mango']
console.log[arrayOfCulinaryFruits]; // ['apricot', 'cherry', 'peach', 'plum', 'mango']
8 [hoặc giá trị trung thực] và loại trừ các phần tử mà hàm gọi lại trả về const arrayOfFruits = ['olive', 'apricot', 'cherry', 'peach', 'plum', 'mango'];
const [ , ...arrayOfCulinaryFruits] = arrayOfFruits;
// arrayOfFruits is unchanged
console.log[arrayOfFruits]; // ['olive', 'apricot', 'cherry', 'peach', 'plum', 'mango']
console.log[arrayOfCulinaryFruits]; // ['apricot', 'cherry', 'peach', 'plum', 'mango']
9 [hoặc giá trị giả]Trong trường hợp này, cuộc gọi lại kiểm tra
const arrayOfNumbers = [1, 2, 3, 4];
const previousLastElementOfTheArray = arrayOfNumbers.pop[];
console.log[arrayOfNumbers]; // [1, 2, 3]
console.log[previousLastElementOfTheArray]; // 4
0 để nó trả về const arrayOfFruits = ['olive', 'apricot', 'cherry', 'peach', 'plum', 'mango'];
const [ , ...arrayOfCulinaryFruits] = arrayOfFruits;
// arrayOfFruits is unchanged
console.log[arrayOfFruits]; // ['olive', 'apricot', 'cherry', 'peach', 'plum', 'mango']
console.log[arrayOfCulinaryFruits]; // ['apricot', 'cherry', 'peach', 'plum', 'mango']
9 cho chữ cái const arrayOfFruits = ['olive', 'apricot', 'cherry', 'peach', 'plum', 'mango'];
const [ , ...arrayOfCulinaryFruits] = arrayOfFruits;
// arrayOfFruits is unchanged
console.log[arrayOfFruits]; // ['olive', 'apricot', 'cherry', 'peach', 'plum', 'mango']
console.log[arrayOfCulinaryFruits]; // ['apricot', 'cherry', 'peach', 'plum', 'mango']
6 và const arrayOfFruits = ['olive', 'apricot', 'cherry', 'peach', 'plum', 'mango'];
const [ , ...arrayOfCulinaryFruits] = arrayOfFruits;
// arrayOfFruits is unchanged
console.log[arrayOfFruits]; // ['olive', 'apricot', 'cherry', 'peach', 'plum', 'mango']
console.log[arrayOfCulinaryFruits]; // ['apricot', 'cherry', 'peach', 'plum', 'mango']
8 cho tất cả những cái khác, dẫn đến một mảng không bao gồm chữ cái const arrayOfFruits = ['olive', 'apricot', 'cherry', 'peach', 'plum', 'mango'];
const [ , ...arrayOfCulinaryFruits] = arrayOfFruits;
// arrayOfFruits is unchanged
console.log[arrayOfFruits]; // ['olive', 'apricot', 'cherry', 'peach', 'plum', 'mango']
console.log[arrayOfCulinaryFruits]; // ['apricot', 'cherry', 'peach', 'plum', 'mango']
6Cuộc gọi lại tới
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
3 được thông qua ba đối số, theo thứ tự. bản thân phần tử, chỉ mục của phần tử và toàn bộ mảngBạn có thể tạo các điều kiện phức tạp hơn ví dụ này, phức tạp như bạn cần
Xóa một phần tử khỏi mảng bằng vòng lặp const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
4 và const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
5
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
Phương thức cuối cùng để loại bỏ một phần tử khỏi mảng mà không làm thay đổi mảng ban đầu là sử dụng phương thức
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
5Với các bước đơn giản này
- Tạo một mảng trống
- Lặp qua mảng ban đầu
- Đẩy vào mảng trống các phần tử bạn muốn giữ lại
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const arrayWithoutB = [];
for [let i = 0; i < arrayOfLetters.length; i++] {
if [arrayOfLetters[i] !== 'b'] {
arrayWithoutH.push[arrayOfLetters[i]];
}
}
// arrayOfLetters is unchanged
console.log[arrayOfLetters]; // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[arrayWithoutB]; // ['a', 'c', 'd', 'e', 'f']
Điều kiện của câu lệnh
const arrayOfNumbers = [1, 2, 3, 4];
const previousLastElementOfTheArray = arrayOfNumbers.pop[];
console.log[arrayOfNumbers]; // [1, 2, 3]
console.log[previousLastElementOfTheArray]; // 4
9 có thể kiểm tra cả chỉ số [const arrayOfNumbers = [1, 2, 3, 4];
const previousFirstElementOfTheArray = arrayOfNumbers.shift[];
console.log[arrayOfNumbers]; // [2, 3, 4]
console.log[previousFirstElementOfTheArray]; // 1
0] và giá trị của phần tử đối với các câu lệnh phức tạp hơnLoại bỏ phần tử đầu tiên của một mảng bằng hàm hủy và toán tử còn lại
Phá hủy mảng và toán tử còn lại là hai khái niệm hơi khó hiểu
Tôi đề xuất bài viết này đề cập đến cách hủy cấu trúc một mảng nếu bạn muốn tìm hiểu sâu hơn về chủ đề này
Bạn có thể xóa phần tử đầu tiên bằng cách hủy – giả sử một mảng có tên là
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const halfBeforeTheUnwantedElement = arrayOfLetters.slice[0, 2]
const halfAfterTheUnwantedElement = arrayOfLetters[3];
const copyWithoutThirdElement = halfBeforeTheUnwantedElement.concat[halfAfterTheUnwantedElement];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutFifthElement] // ['a', 'b', 'd', 'e', 'f']
7 – và tạo một mảng mới có tên là const arrayOfNumbers = [1, 2, 3, 4];
const previousFirstElementOfTheArray = arrayOfNumbers.shift[];
console.log[arrayOfNumbers]; // [2, 3, 4]
console.log[previousFirstElementOfTheArray]; // 1
2 theo cách này. const arrayOfNumbers = [1, 2, 3, 4];
const previousFirstElementOfTheArray = arrayOfNumbers.shift[];
console.log[arrayOfNumbers]; // [2, 3, 4]
console.log[previousFirstElementOfTheArray]; // 1
3Bây giờ, hãy xem một ví dụ thực tế về cách sử dụng hàm hủy và toán tử còn lại
const arrayOfFruits = ['olive', 'apricot', 'cherry', 'peach', 'plum', 'mango'];
const [ , ...arrayOfCulinaryFruits] = arrayOfFruits;
// arrayOfFruits is unchanged
console.log[arrayOfFruits]; // ['olive', 'apricot', 'cherry', 'peach', 'plum', 'mango']
console.log[arrayOfCulinaryFruits]; // ['apricot', 'cherry', 'peach', 'plum', 'mango']
Đặt dấu phẩy trước toán tử còn lại nói để tránh phần tử đầu tiên trong mảng và tất cả các phần tử khác được sao chép trong mảng
const arrayOfNumbers = [1, 2, 3, 4];
const previousFirstElementOfTheArray = arrayOfNumbers.shift[];
console.log[arrayOfNumbers]; // [2, 3, 4]
console.log[previousFirstElementOfTheArray]; // 1
4Cách xóa phần tử khỏi mảng trong khi thay đổi mảng
Trong một số trường hợp, có thể thích hợp để thay đổi mảng ban đầu. Trong những trường hợp này, bạn cũng có thể sử dụng một trong các phương pháp đột biến sau
5const arrayOfNumbers = [1, 2, 3, 4]; const previousFirstElementOfTheArray = arrayOfNumbers.shift[]; console.log[arrayOfNumbers]; // [2, 3, 4] console.log[previousFirstElementOfTheArray]; // 1
6const arrayOfNumbers = [1, 2, 3, 4]; const previousFirstElementOfTheArray = arrayOfNumbers.shift[]; console.log[arrayOfNumbers]; // [2, 3, 4] console.log[previousFirstElementOfTheArray]; // 1
7const arrayOfNumbers = [1, 2, 3, 4]; const previousFirstElementOfTheArray = arrayOfNumbers.shift[]; console.log[arrayOfNumbers]; // [2, 3, 4] console.log[previousFirstElementOfTheArray]; // 1
Xóa phần tử cuối cùng của một mảng bằng const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
6
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
Bạn có thể xóa phần tử cuối cùng của một mảng bằng
const arrayOfNumbers = [1, 2, 3, 4];
const previousFirstElementOfTheArray = arrayOfNumbers.shift[];
console.log[arrayOfNumbers]; // [2, 3, 4]
console.log[previousFirstElementOfTheArray]; // 1
5Nếu bạn có một mảng tên là
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const halfBeforeTheUnwantedElement = arrayOfLetters.slice[0, 2]
const halfAfterTheUnwantedElement = arrayOfLetters[3];
const copyWithoutThirdElement = halfBeforeTheUnwantedElement.concat[halfAfterTheUnwantedElement];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutFifthElement] // ['a', 'b', 'd', 'e', 'f']
7, thì có vẻ như là const arrayOfNumbers = [1, 2, 3, 4];
const previousSecondElementOfTheArray = arrayOfNumbers.splice[1, 1];
console.log[arrayOfNumbers]; // [1, 3, 4]
console.log[previousSecondElementOfTheArray]; // [2]
1const arrayOfNumbers = [1, 2, 3, 4];
const previousLastElementOfTheArray = arrayOfNumbers.pop[];
console.log[arrayOfNumbers]; // [1, 2, 3]
console.log[previousLastElementOfTheArray]; // 4
Phương thức
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
6 được sử dụng trên mảng và nó thay đổi mảng bằng cách xóa phần tử cuối cùng của mảngPhương thức
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
6 cũng trả về phần tử đã loại bỏXóa phần tử đầu tiên của một mảng bằng const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
7
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
Phương thức
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
7 có thể được sử dụng trên một mảng để loại bỏ phần tử đầu tiên của một mảngNếu bạn có một mảng tên là
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const halfBeforeTheUnwantedElement = arrayOfLetters.slice[0, 2]
const halfAfterTheUnwantedElement = arrayOfLetters[3];
const copyWithoutThirdElement = halfBeforeTheUnwantedElement.concat[halfAfterTheUnwantedElement];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutFifthElement] // ['a', 'b', 'd', 'e', 'f']
7 thì nó có thể được sử dụng theo cách này. const arrayOfNumbers = [1, 2, 3, 4];
const previousSecondElementOfTheArray = arrayOfNumbers.splice[1, 1];
console.log[arrayOfNumbers]; // [1, 3, 4]
console.log[previousSecondElementOfTheArray]; // [2]
7const arrayOfNumbers = [1, 2, 3, 4];
const previousFirstElementOfTheArray = arrayOfNumbers.shift[];
console.log[arrayOfNumbers]; // [2, 3, 4]
console.log[previousFirstElementOfTheArray]; // 1
Phương thức
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
7 loại bỏ phần tử đầu tiên của mảngNó cũng trả về phần tử đã loại bỏ
Xóa một phần tử tại bất kỳ chỉ mục nào với const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
8
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
Bạn có thể xóa phần tử tại bất kỳ chỉ mục nào bằng cách sử dụng phương thức
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
8Nếu bạn có một mảng tên là
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const halfBeforeTheUnwantedElement = arrayOfLetters.slice[0, 2]
const halfAfterTheUnwantedElement = arrayOfLetters[3];
const copyWithoutThirdElement = halfBeforeTheUnwantedElement.concat[halfAfterTheUnwantedElement];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutFifthElement] // ['a', 'b', 'd', 'e', 'f']
7, nó có thể được sử dụng theo cách này để loại bỏ một phần tử tại bất kỳ chỉ mục nào. slice
2, với const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const arrayWithoutB = [];
for [let i = 0; i < arrayOfLetters.length; i++] {
if [arrayOfLetters[i] !== 'b'] {
arrayWithoutH.push[arrayOfLetters[i]];
}
}
// arrayOfLetters is unchanged
console.log[arrayOfLetters]; // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[arrayWithoutB]; // ['a', 'c', 'd', 'e', 'f']
8 là chỉ số của phần tử cần xóaconst arrayOfNumbers = [1, 2, 3, 4];
const previousSecondElementOfTheArray = arrayOfNumbers.splice[1, 1];
console.log[arrayOfNumbers]; // [1, 3, 4]
console.log[previousSecondElementOfTheArray]; // [2]
Phương thức
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
8 có thể chấp nhận nhiều đối sốĐể xóa một phần tử tại bất kỳ chỉ mục nào, bạn cần cung cấp cho
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
8 hai đối số. đối số đầu tiên là chỉ số của phần tử cần xóa, đối số thứ hai là số lượng phần tử cần xóaVì vậy, nếu bạn có một mảng tên là
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const halfBeforeTheUnwantedElement = arrayOfLetters.slice[0, 2]
const halfAfterTheUnwantedElement = arrayOfLetters[3];
const copyWithoutThirdElement = halfBeforeTheUnwantedElement.concat[halfAfterTheUnwantedElement];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutFifthElement] // ['a', 'b', 'd', 'e', 'f']
7, để loại bỏ một phần tử ở chỉ số 4, cách sử dụng phương thức const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
8 sẽ là. slice
8Phương thức
const arrayOfLetters = ['a', 'b', 'c', 'd', 'e', 'f'];
const copyWithoutLastElement = arrayOfLetters.slice[0, -1];
// arrayOfLetters is unchanged
console.log[arrayOfLetters] // ['a', 'b', 'c', 'd', 'e', 'f']
console.log[copyWithoutLastElement] // ['a', 'b', 'c', 'd', 'e']
8 sau đó trả về một mảng chứa các phần tử đã loại bỏPhần kết luận
Có nhiều cách khác nhau để làm điều tương tự trong JavaScript
Trong bài viết này, bạn đã học được chín phương pháp khác nhau để loại bỏ một phần tử khỏi mảng. Sáu trong số chúng không làm thay đổi mảng ban đầu và ba trong số chúng làm
Bạn có thể sẽ sử dụng tất cả chúng vào lúc này hay lúc khác, và có thể bạn sẽ học được nhiều phương pháp hơn nữa để làm điều tương tự
Chúc vui vẻ
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
Người điều hành và tác giả nhân viên cho freeCodeCamp
Nếu bạn đọc đến đây, hãy tweet cho tác giả để cho họ thấy bạn quan tâm. Tweet một lời cảm ơn
Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu