Comparison summary of array slice and splice in JavaScript
Preface
I reviewed Javascript today and saw the array methods. There are two similar methods - splice and splice. They look very similar, but there is an extra p, but the usage But quite different.
In use, you can reduce the occurrence of confusion by choosing an API with strong semantic expression.
1. slice
slice specifies the elements in an array to create a new array, that is, the original array will not change the slice of the
array (ECMAScript 5.1 Standard 15.4 .4.10) is very similar to a slice of strings. According to the specification, slice requires two parameters, the start point and the end point. It returns a new array containing all elements from the start point to the end point.
It is not too difficult to understand the function of slice:
'abc'.slice(1,2) // "b" [14, 3, 77].slice(1, 2) // [3]
It is important to note that it does not modify the original array.
The following code snippet describes this behavior. The value of x has not changed, and y is the intercepted part.
var x = [14, 3, 77]; var y = x.slice(1, 2); console.log(x); // [14, 3, 77] console.log(y); // [3]
2, splice
splice is the most powerful array method in JS. It can implement deletion, insertion, and replacement operations on array elements, and the return value is the operated value.
splice deletion: color.splice(1,2) (delete 1 and 2 in color);
splice insertion: color.splice(1,0,'brown', 'pink') (insert two values before the element with color key value 1);
splice replacement: color.splice(1,2,'brown','pink') (replace in color 1, 2 elements);
Although splice (section 15.4.4.12) also requires (at least) two parameters, its meaning is completely different.
[14, 3, 77].slice(1, 2) // [3] [14, 3, 77].splice(1, 2) // [3, 77]
In addition, splice will also change the original array.
Don’t be too surprised, this is exactly what splice is intended to do.
var x = [14, 3, 77] var y = x.splice(1, 2) console.log(x) // [14] console.log(y) // [3, 77]
When you write your own module, it is important to choose an API that is least likely to be confused. In theory, your users shouldn't always be able to figure out which one they need by reading the documentation. So which naming convention should we follow?
The convention I'm most familiar with (related to my previous experience with QT) is to choose the verb correctly: present tense indicates possible modification behavior, past tense does not modify the original object, but returns a new one version of. If possible, provide both versions.
Refer to the following example:
var p = new Point(100, 75); p.translate(25, 25); console.log(p); // { x: 125, y: 100 } var q = new Point(200, 100); var s = q.translated(10, 50); console.log(q); // { x: 200, y: 100 } console.log(s); // { x: 210, y: 150 }
Note the difference between translate() that moves the position (in the two-dimensional Cartesian coordinate system) and translated() that only creates a moved coordinate the difference. Calling translate modifies the value of point p. However, because translated() does not modify the original object, object q is not modified, but only a new copy s is returned.
Summary
If this specification can be deployed very consistently across your applications, the confusion mentioned above will be minimized. The above is the entire content of this article. I hope it can bring some help to everyone's study or work.
For more articles related to the comparison of array slice and splice in JavaScript, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
