


Js array operations push, pop, shift, unshift and other methods are introduced in detail_javascript skills
There are still many methods for array operations in js. Today I suddenly thought of summarizing them, which can be regarded as reviewing the past and learning the new. However, I will not explain every method, I will just select some of them.
First of all, let’s talk about the push and pop methods. These two methods will only push or pop the array from the end, and operate on the original array. Any changes will affect the operated array. push(args) can push multiple elements at a time and return the updated array length. The pop() function will only pop up the last element at the end each time and return the popped element. If pop() is called on an empty array, it will return undefined. If the parameter is an array, the entire array is pushed into the original array as one element. It will not produce the "splitting phenomenon" similar to when concat merges arrays. Let's look at the example below
Example 1:
var oldArr=[1,2,3];
alert(oldArr.push(4,[5,6]))–>5 (only [5,6] will be calculated as one element here, and the updated array length 5 will be returned)
At this time oldArr–>[1,2,3,4,[5,6]]
alert(oldArr.pop())–>[5,6](The last element [5,6] pops here, Instead of 6)
oldArr–>[1,2,3,4]
oldArr.pop()–>4
oldArr.pop()–>3
oldArr .pop()–>2
oldArr.pop()–>1
oldArr.pop()–>undefined (empty array popup)
Now after talking about push and pop, let’s look at unshift and shift
, both methods operate on the head of the array. Others are basically similar to push and pop, but in IE, the unshift method returns undefined
Example 2 :
var oldArr2=[1,2];
oldArr2.unshift(3)–>undefined
At this time oldArr2 is–>[3,1,2]
oldArr2 .shift()–>3
At this time, oldArr2 is [1,2]
Let’s take a look at the more powerful splice, which can be used to add and delete elements at random positions in the array. Its operation is also in the original There are
Modify on the array
splice(start,deleteCnt,args) The start in splice(start,deleteCnt,args) means to start the operation subscript, deleteCnt means to delete starting from the start subscript (including the element) The number of elements, the deletion operation returns the deleted elements. args represents the elements used to replace deleted elements (can have multiple parameters). start and deleteCnt must be numbers. If they are not numbers, try to convert them. If the conversion fails, it will be treated as 0. splice must have at least one start element, otherwise no operation will be performed. The absence of deleteCnt means deleting start and all subsequent elements (under IE, 0 will not be deleted). start can be a negative number, which means starting from the end of the right side of the array. If deleteCnt is negative, no deletion will be performed because it is impossible to delete negative elements.
Now that the explanation is over, let’s take a look at an example. You may be able to understand it better through examples
Example 3:
var oldArr3=[1,2];
oldArr3.splice()–>””(returns an empty string, does not perform any operation, after the operation oldArr3–>[1,2])
oldArr3.splice(“”)–> [1,2]("" failed to convert to a number and returned 0, so delete 1,2, oldArr3–>[] after the operation, but it is a bit disgusting under IE and does not do any operation)
oldArr3.splice(" 1a")–>Same as above
odlArr3.splice(0,2)–>[1,2]("Starting from the element with subscript 0, delete two elements 1,2, so after deletion oldArr3–> [])
oldArr3.splice(0,-1)–>””(Delete -1 elements starting from the 0 subscript, so no operation is done. After the operation, oldArr3–>[1,2] )
oldArr3.splice(1,1)–>2 (Delete 1 element starting from index 1, that is, delete 2, so after deletion, oldArr3–>[1])
oldArr3.splice(1 ,4)–>2 (Delete 4 elements starting from subscript 1, and there is only 1 element starting from 1, so delete 2, so after deletion, oldArr3–>[1])
oldArr3.splice(-1, 0,3)–>””(Delete 0 elements starting from subscript -1, which is element 2, and then add element 3, so after the operation oldArr3–>[1,3,2])
oldArr3.splice (-1,1,3)–>2 (Delete 1 element starting from subscript -1, which is 2 elements, and then add element 3. After the operation, it is oldArr3–>[1,3])
OK Next Let's start with concat. This method is used to connect two or more arrays. The array will not change the original array and will only return a new array. If the parameter is an array during the connection, the elements in the array will be connected. It is relatively simple to start the example directly
Example 4:
var oldArr4=[1,2];
oldArr4.concat(3,4)–>[1,2 ,3,4]
oldArr4.concat(3,4,[5,6])–>[1,2,3,4,5,6](What is added here is [5,6] Element 5 and element 6)
oldArr4.concat(3,[4,[5,6]])–>[1,2,3,4,[5,6]](the innermost layer here The entire elements [5,6] are used to add, not to split)
Let’s talk about the sorting method in the array sort
sort(function) is to sort the original array and will not generate a new array . By default, sort() without parameters converts the elements in the array into strings for comparison. When comparing, the characters are sorted according to the order in the character encoding. Each character has a unique encoding corresponding to it.
Look at the following example
var oldArr5=[3,1,5,7,17] Looking at this general concept, when sorting oldArr5, oldArr5.sort() will follow Sorting the numbers from small to large returns [1,3,5,7,17], but if you look at the result, it actually returns [1,17,3,5,7] because they are converted into strings during comparison. Then compare the strings one by one. If the first character is the same, compare the second character. Otherwise, the comparison result will be returned directly. Because "17"
In addition to the default no parameters, the sort(function) method can also pass in a custom sorting method. In this way, the sorting results can be completely controlled by yourself. You can sort them however you want. Isn’t it great? Ah, hehe. Generally, a custom function comparison function contains two parameters representing the left element and the right element used for comparison. Then a result is returned in a certain way. If the return value is greater than 0, it means that the left and right elements are exchanged. If the return value is less than 0 or equal to 0, it means that the left and right elements will not be exchanged. Now look at the example
Example 5:
Arrange the original array according to the numbers from large to small
var oldArr5=[3,1,5,7,17]; //Initial array
function mySort(left,right) {
if(left
else{
return -1;}//If the left element is greater than or equal to The elements on the right are not exchanged
}
Of course the above method can be simplified to funaction mySort(left,right){ return right-left;}
//Sort by even numbers first and odd numbers last
var oldArr6=[3,6,7, 18];//Initial array
function mySort2(left,right){
if(left%2==0)return -1;//If the left element is an even number, no exchange is done
if(right %2==0)return 1; //If the right element is an even number, exchange
return 0; //Do not exchange
}
I won’t talk much about the last slice, just use To intercept some elements in the original array and return a new array. The original array will not change. Its operation method is similar to the slice of string
var oldArr7=[1,2,3,4];
oldArr7.slice(0)–>[1,2,3,4 ]
oldArr7.slice(0,2)–>[1,2]
oldArr7.slice(0,0)–>[]
oldArr7.slice(0,-1)–> ;[1,2,3]
oldArr7.slice(-3,-1)–>[2,3]
oldArr4.slice(-1,-3)–[]

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.


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

Dreamweaver CS6
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment
