Home >Web Front-end >JS Tutorial >What are the methods to delete elements from js array

What are the methods to delete elements from js array

WBOY
WBOYOriginal
2024-02-19 15:34:05474browse

What are the methods to delete elements from js array

There are many ways to delete elements from js arrays, including using the splice() method, using the pop() and shift() methods, using the delete keyword, and using the filter() method. wait. The following will introduce you to these methods in detail and provide specific code examples.

  1. Use the splice() method to delete elements
    The splice() method can delete elements in the array at the same time and return the deleted elements. The syntax is: array.splice(start, deleteCount, item1, item2, ...);
  2. start: Specifies the index of the starting position of the deleted element.
  3. deleteCount: Specify the number of elements to be deleted.
  4. item1, item2, ...: Optional parameters, new elements added to the array.

The sample code is as follows:

let fruits = ['apple', 'banana', 'orange', 'grape'];
fruits.splice(1, 2); // 从索引为1的元素开始删除2个元素
console.log(fruits); // 输出:['apple', 'grape']
  1. Use the pop() and shift() methods to delete elements
    The pop() method is used to delete the last element of the array, and returns the deleted element. The shift() method is used to delete the first element of the array and return the deleted element. Both methods require no parameters.

The sample code is as follows:

let fruits = ['apple', 'banana', 'orange', 'grape'];
fruits.pop(); // 删除最后一个元素
console.log(fruits); // 输出:['apple', 'banana', 'orange']

fruits.shift(); // 删除第一个元素
console.log(fruits); // 输出:['banana', 'orange']
  1. Use the delete keyword to delete elements
    The delete keyword is used to delete the element at the specified position, but it does not change the array length. Deleted elements will become undefined. It should be noted that using the delete keyword cannot maintain the continuity of the array, that is, holes will appear in the array.

The sample code is as follows:

let fruits = ['apple', 'banana', 'orange', 'grape'];
delete fruits[1]; // 删除索引为1的元素
console.log(fruits); // 输出:['apple', undefined, 'orange', 'grape']
  1. Use the filter() method to delete elements
    The filter() method will create a new array that contains all the elements in the array specified by Elements of function testing. We can delete elements that do not meet the condition by specifying a conditional function.

The sample code is as follows:

let fruits = ['apple', 'banana', 'orange', 'grape'];
fruits = fruits.filter(function(fruit) {
    return fruit !== 'banana'; // 删除值为'banana'的元素
});
console.log(fruits); // 输出:['apple', 'orange', 'grape']

The above are the specific instructions and sample codes for several methods of deleting elements from js arrays. Different methods are suitable for different scenarios, and you can choose the appropriate method according to your needs when using it.

The above is the detailed content of What are the methods to delete elements from js array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn