Home > Web Front-end > JS Tutorial > Distinguish between slice method and splice method

Distinguish between slice method and splice method

WBOY
Release: 2024-02-18 23:11:12
Original
643 people have browsed it

Distinguish between slice method and splice method

The difference between the slice method and the splice method requires specific code examples

In JavaScript, an array is a commonly used data structure that allows us to store multiple values. , and access and modify these values ​​through indexes. When operating an array, we often encounter situations where we need to intercept a part of the array or delete/add elements of the array. JavaScript provides two methods for operating arrays, the slice method and the splice method, which are different in function.

First, let’s look at the slice method. This method can return a new array by specifying the start index and the end index, which contains the elements from the start index to the end index in the original array (excluding the elements corresponding to the end index). The slice method does not modify the original array, but returns a new copy of the array.

The following is a sample code using the slice method:

const fruits = ['apple', 'banana', 'orange', 'grape', 'watermelon'];

// 从索引1开始,到索引3结束(不包含索引3)
const slicedFruits = fruits.slice(1, 3);

console.log(slicedFruits); // 输出: ['banana', 'orange']
console.log(fruits); // 输出: ['apple', 'banana', 'orange', 'grape', 'watermelon']
Copy after login

In the above code, we use the slice method to intercept the elements from the original array fruits from index 1 to index 3. Got a new array slicedFruits. Note that the original array fruits has not changed, it still contains all elements.

Next, let’s look at the splice method. This method modifies the array by specifying the starting index, the number of elements to be removed, and the elements to be added. The splice method directly modifies the original array instead of returning a new copy of the array.

The following is a sample code using the splice method:

const fruits = ['apple', 'banana', 'orange', 'grape', 'watermelon'];

// 从索引1开始删除2个元素,并添加'pear'和'kiwi'
fruits.splice(1, 2, 'pear', 'kiwi');

console.log(fruits); // 输出: ['apple', 'pear', 'kiwi', 'grape', 'watermelon']
Copy after login

In the above code, we use the splice method to delete 2 elements starting from index 1 in the original array fruits, and add 'pear' and 'kiwi'. As you can see, the original array fruits has changed and its elements have been modified.

Summary:

  • The slice method intercepts a part of the array and returns a new copy of the array without modifying the original array;
  • The splice method modifies the array. Elements can be deleted and added, and the original array can be modified directly.

By comparing the slice method and the splice method, we can choose which method to use to operate the array according to specific needs.

The above is the detailed content of Distinguish between slice method and splice method. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template