Home > Web Front-end > JS Tutorial > How to use splice in js

How to use splice in js

下次还敢
Release: 2024-05-01 04:06:18
Original
735 people have browsed it

JavaScript's Splice method allows you to delete or insert elements from an array, returning a new array containing the deleted elements. The usage is as follows: Delete elements: arr.splice(start, deleteCount) Insert elements: arr.splice(start, 0, ...items) Replace elements: arr.splice(start, 1, ...items) Delete from the end Element: arr.splice(-1, deleteCount)Delete element from the beginning: arr.splice(0, deleteCount)

How to use splice in js

Splice method in JavaScript Usage

The Splice method is a built-in method of the Array object in JavaScript, used to delete or insert elements from the array. It returns a new array containing the deleted elements, and new elements can be inserted in the original order.

Syntax

<code class="js">array.splice(start, deleteCount, ...items)</code>
Copy after login

Parameters

  • start: The starting position of deleted elements , starting from 0. If you specify a negative number, counting starts from the end of the array.
  • deleteCount: The number of elements to be deleted. If not specified, it will be deleted from the starting position to the end of the array.
  • items: Optional parameter used to insert new elements after deletion.

Return value

A new array containing the deleted elements.

How to remove elements using

  1. :

    <code class="js">const arr = [1, 2, 3, 4, 5];
    arr.splice(2, 2); // [3, 4]</code>
    Copy after login
  2. Insert element:

    <code class="js">arr.splice(2, 0, 'a', 'b'); // [1, 2, 'a', 'b', 3, 4, 5]</code>
    Copy after login
  3. Replace element:
    Delete element and insert new element, which is equivalent to replacement operation:

    <code class="js">arr.splice(2, 1, 'c'); // [1, 2, 'c', 4, 5]</code>
    Copy after login
  4. Remove elements from the end of the array:
    Use a negative starting position:

    <code class="js">arr.splice(-1, 1); // [1, 2, 'c', 4]</code>
    Copy after login
  5. Remove elements from the beginning of the array :
    Specify the starting position as 0:

    <code class="js">arr.splice(0, 1); // [2, 'c', 4]</code>
    Copy after login

Note

  • The Splice method will directly modify the original array.
  • The returned array contains the deleted elements, and the original array will be modified.
  • If deleteCount is less than 0 or greater than the array length, it will be ignored.
  • If start is greater than the array length, no operation will be performed.
  • If you want to delete and insert elements at the same time, you should use the spread operator (...) with the new element as argument.

The above is the detailed content of How to use splice in js. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template