Cách xóa sản phẩm trong javascript?

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ư

  • 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
  • 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 cùng với
    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']
    1
  • 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
  • Một 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 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']
    4

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ên

Xó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']
5

Dướ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 sao

Loạ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
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

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

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']

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

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']
6

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']

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']
6

Cuộ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ảng

Bạ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

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']
5

Với các bước đơn giản này

  1. Tạo một mảng trống
  2. Lặp qua mảng ban đầu
  3. Đẩ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ơn

Loạ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
3

Bâ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
4

Cá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

  • const arrayOfNumbers = [1, 2, 3, 4];
    
    const previousFirstElementOfTheArray = arrayOfNumbers.shift[];
    
    console.log[arrayOfNumbers]; // [2, 3, 4]
    
    console.log[previousFirstElementOfTheArray]; // 1
    5
  • const arrayOfNumbers = [1, 2, 3, 4];
    
    const previousFirstElementOfTheArray = arrayOfNumbers.shift[];
    
    console.log[arrayOfNumbers]; // [2, 3, 4]
    
    console.log[previousFirstElementOfTheArray]; // 1
    6
  • const arrayOfNumbers = [1, 2, 3, 4];
    
    const previousFirstElementOfTheArray = arrayOfNumbers.shift[];
    
    console.log[arrayOfNumbers]; // [2, 3, 4]
    
    console.log[previousFirstElementOfTheArray]; // 1
    7

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

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
5

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, 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]
1

const 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ả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']
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

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ảng

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 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]
7

const 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ảng

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

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']
8

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, 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. slice2, 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óa

const 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óa

Vì 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à. slice8

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

Ilenia Magoni

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

Làm cách nào để xóa mục khỏi đối tượng trong JavaScript?

Xóa Thuộc tính khỏi Đối tượng . Sau khi xóa, tài sản không thể được sử dụng trước khi nó được thêm lại. Toán tử xóa được thiết kế để sử dụng trên các thuộc tính đối tượng. Nó không ảnh hưởng đến các biến hoặc chức năng. The delete operator deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again. The delete operator is designed to be used on object properties. It has no effect on variables or functions.

Làm cách nào để xóa mục danh sách bằng JavaScript?

Xóa mục danh sách bằng JavaScript. Để xóa một mục danh sách, gọi hàm deleteObject[] trên đối tượng . Ví dụ sau sử dụng hàm getItemById[id] để trả về mục thứ hai từ danh sách, sau đó xóa mục đó.

Làm cách nào để thêm và xóa các mục trong JavaScript?

Các mục trong danh sách được thêm hoặc xóa bằng các hàm JavaScript addItem[] và removeItem[] . Các mục danh sách được tạo bằng tài liệu. phương thức createElement[] và để tạo một nút văn bản, tài liệu. phương thức createTextNode[] được sử dụng và sau đó nút này được nối thêm bằng phương thức appendChild[].

Có chức năng xóa trong JavaScript không?

Toán tử xóa xóa thuộc tính khỏi đối tượng . Nếu giá trị của thuộc tính là một đối tượng và không còn tham chiếu đến đối tượng, thì đối tượng được giữ bởi thuộc tính đó cuối cùng sẽ tự động được giải phóng.

Chủ Đề