当前位置:网站首页>Array operations in JS
Array operations in JS
2022-06-13 06:35:00 【Sindyue】
Declare objects 、 New elements 、 Remove elements 、 Additional elements 、 Modifying elements 、 clear list
1. Declare objects
var arrayObject=[];
var arrayObject=new Array();
var arrayObject=new Array(size);
var arrayObject=new Array(element0, element1, ..., elementn);
2. New elements
2.1 unshift() Method to add one or more elements to the beginning of an array , And returns the new length .
grammar
arrayObject.unshift(newelement1,newelement2,....,newelementX)
Parameters describe
newelement1 It's necessary . The first element added to the array .
newelement2 Optional . The second element added to the array .
newelementX Optional . Several elements can be added .
explain
unshift() Method will insert its parameters into arrayObject The head of , And move the existing elements to the higher subscript successively , To make room for . The first parameter of this method will be the new element of the array 0, If there's a second parameter , It's going to be a new element 1, And so on .
Please note that ,unshift() Method does not create a new array , Instead, modify the original array directly .unshift() Methods cannot be used in Internet Explorer To work correctly in !
2.2 push() Method to add one or more elements to the end of an array , And returns the new length .
grammar
arrayObject.push(newelement1,newelement2,....,newelementX)
explain
push() Method to add its parameter order to arrayObject Tail of . It directly modifies arrayObject, Instead of creating a new array .push() Methods and pop() Method to use the advanced post stack function provided by array .
Tips : This method changes the length of the array .
3. Remove elements
3.1 shift() Method is used to remove the first element of an array , And returns the value of the first element .
grammar
arrayObject.shift()
Return value
The value of the first element of the original array .
explain
If the array is empty , that shift() Method will do nothing , return undefined value . Please note that , This method does not create a new array , But directly modify the original arrayObject.
notes :
This method changes the length of the array .
3.2 pop() Method is used to delete and return the last element of the array .
grammar
arrayObject.pop()
Return value
arrayObject Last element of .
explain
pop() Method will delete arrayObject Last element of , Reduce the length of the array by 1, And return the value of the element it deleted . If the array is already empty , be pop() Don't change the array , And back to undefined value .
4. Convert numeric values to strings
4.1 join() Method is used to put all elements in an array into a string .
The elements are separated by the specified separator .
grammar
arrayObject.join(separator)
separator Optional . Specifies the separator to use . If this parameter is omitted , Then use comma as separator .
Return value
Returns a string . The string is created by putting arrayObject Each element of is converted to a string , And then connect these strings , Insert... Between two elements separator String .
4.2 toString() Method converts an array to a string , And return the result .
grammar
arrayObject.toString()
Return value
arrayObject String representation of . The return value is the same as that without parameters join() Method returns the same string .
5. splice() Method
Definition and Usage
splice() Method direction / Add from the array / Delete the project , Then return the deleted item .
notes :
This method changes the original array .
grammar
arrayObject.splice(index,howmany,item1,.....,itemX)
Parameters describe
index It's necessary . Integers , Prescribed addition / Delete project location , Use negative numbers to specify the position from the end of the array .
howmany It's necessary . Number of items to delete . If set to 0, The item will not be deleted .
item1, ..., itemX Optional . New items added to array .
Return value
Array: New array containing deleted items , If any .
explain
splice() Method can be deleted from index Zero or more elements starting at , And replace the deleted elements with one or more values declared in the parameter list .
If from arrayObject Element... Is removed from , Then it returns an array of deleted elements .
notes :
Please note that ,splice() Methods and slice() The function of methods is different ,splice() Method will directly modify the array .
6. slice() Method
Definition and Usage
slice() Method to return selected elements from an existing array .
grammar
arrayObject.slice(start,end)
Parameters and descriptions
start It's necessary .
Specify where to start selecting . If it's a negative number , So it specifies the position from the end of the array . in other words ,-1 Last element ,-2 The second last element , And so on .
end Optional .
Specify where to end the selection . This parameter is the array subscript at the end of the array fragment . If the parameter is not specified , So the sharded array contains from start All elements to the end of the array . If this parameter is negative , So it specifies the elements starting from the end of the array .
Return value
Returns a new array , Contains from start To end ( This element is not included ) Of arrayObject The elements in .
explain
Please note that , This method does not modify the array , Instead, it returns a sub array . If you want to delete an element in an array , Should use method Array.splice().
Tips and notes
notes : You can use negative values to select elements from the end of the array .
notes : If end Not regulated , that slice() Method will be selected from start All elements to the end of the array .
7. concat() Method
Definition and Usage
concat() Method to connect two or more arrays .
This method does not change the existing array , Only one copy of the connected array will be returned .
grammar
arrayObject.concat(arrayX,arrayX,......,arrayX)
Parameters describe
arrayX It's necessary . This parameter can be a specific value , It can also be an array object . It could be any number .
Return value
Returns a new array . The array is created by putting all arrayX Parameters added to arrayObject Generated in the . If you want to do concat() The parameters of the operation are arrays , Then you add the elements in the array , Not an array .
In this case , We created two arrays , And then use concat() Connect them :
<script type="text/javascript">
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
var arr2 = new Array(3)
arr2[0] = "James"
arr2[1] = "Adrew"
arr2[2] = "Martin"
document.write(arr.concat(arr2))
</script>
Output results
George,John,Thomas,James,Adrew,Martin
Tips :
At this time arr It hasn't changed , It is arr.concat(arr2) The return value of is a list containing both .
Please refer to the link for details : JavaScript Array object https://www.w3school.com.cn/jsref/jsref_obj_array.asp
边栏推荐
- Thread correlation point
- Error in downloading opencv from pip
- Kotlin basic objects, classes and interfaces
- Ijkplayer code walkthrough player network video data reading process details
- Comprehensive overview of ijkplayer contour features for ijkplayer code walk through
- App performance test: (II) CPU
- Construction and verification of Alibaba cloud server webrtc system
- 【新手上路常见问答】一步一步理解程序设计
- MFS explanation (VI) -- MFS chunk server installation and configuration
- App performance test: (III) traffic monitoring
猜你喜欢

JVM基础

Wechat applet custom tabbar (session customer service) vant

电镀挂具RFID工序管理解决方案

MFS details (VII) -- MFS client and web monitoring installation configuration

Detailed explanation of PHP distributed transaction principle

Echart line chart: multiple line charts show only one line at a time

无刷直流电机矢量控制(四):基于滑模观测器的无传感器控制

【2022高考季】作为一个过来人想说的话

Hbuilderx: installation of hbuilderx and its common plug-ins

Analysis of 43 cases of MATLAB neural network: Chapter 11 optimization of continuous Hopfield Neural Network -- optimization calculation of traveling salesman problem
随机推荐
Use of smalidea
[FAQs for novices on the road] about technology management
Cross process two-way communication using messenger
AI realizes "Resurrection" of relatives | old photo repair | old photo coloring, recommended by free app
【sketchup 2021】草图大师中CAD文件的导入与建模(利用cad图纸在草图大师中建立立面模型)、草图大师导出成品为dwg格式的二维、三维、立面效果到cad中打开预览】
Dynamic link library nesting example
《MATLAB 神经网络43个案例分析》:第10章 离散Hopfield神经网络的分类——高校科研能力评价
App performance test: (II) CPU
Applet disable native top
BlockingQueue源码
Ijkplayer code walkthrough player network video data reading process details
【2022高考季】作为一个过来人想说的话
JVM基础
Wechat applet (get location)
Jetpack - basic use of room
Custom view - extensible collapsexpendview
The processing and application of C language to documents
The boys x pubgmobile linkage is coming! Check out the latest game posters
Kotlin collaboration - flow+room database
App performance test: (I) startup time