Detailed explanation of steps to use JS array method

php中世界最好的语言
Release: 2018-05-24 16:06:22
Original
1884 people have browsed it

This time I will bring you a detailed explanation of the steps to use the JS array method. What are theprecautionswhen using the JS array method? The following is a practical case, let’s take a look.

1.join

Function:Splice thearrayinto astring by specifying characters.
Syntax:stringarr.join([separator = ',']);
Parameters:separator can If omitted,defaults to a comma. Ifseprator is an empty string, then all elements in the array will beconcatenateddirectly.
Return value:The concatenated string.

Copy after login
2.push

Function:Add one or more elements to theendof the array.
Syntax:number arr.push(element1, ..., elementN);
Parameters:elementN, one or more elements.
Return value:The length of the new array.
Note:will modify the original array.

Copy after login
3.unshift

Function:Add one or more elements to theheadof the array.
Syntax:number arr.push(element1, ..., elementN);
Parameters:elementN, one or more elements.
Return value:The length of the new array.
Note:will modify the original array.

Copy after login
4.pop

Function:DeletearrayLastaelement.
Syntax:mixed arr.pop();
Return value:The deleted element.
Note:will modify the original array.

Copy after login
5.shift

Function:Delete the firstelementof the array.Syntax:
mixed arr.pop();Return value:
The deleted element.Note:
will modify the original array.

Copy after login
6.concat

Function:

Merge the incomingarray or non-array valuewith theoriginal array, form anew array and return.Syntax:
array array.concat(value1, value2, ..., valueN);Parameters:
valueN refers to the array or non-array value that needs to be merged with the original array.Return value:
Merged arrayNote:
will notmodify the contents of the original array.

Copy after login
7.reverse

Function:

Reverse the position of the elements in the array.Syntax: array arr.reverse( )
Return value:
The array after reversing the orderNote:
willchange the original array.

Copy after login
8.slice

Function:

Intercept a part of the **array and return this new array.

Syntax:
array arr.slice([begin[,end]]);Return value:
The new array ofafter interceptionNote:
Contains thestarting position,does not includeincludes the ending position,will notchange the original array.If nothing is passed in, the original array will be array.

Copy after login
9.splice

Function:

Replace old elements with new elements to modify the contents of the array

Syntax:
array array.splice(start,deleteCount[, item1[, item2[, ...]]])Parameters:

- start 起始位置 - deleteCount 删除长度 - item 添加的内容
Copy after login
Return value: An

array composed of the deleted elementsNote:
modifies the contents of the original array.

Copy after login

10.sort

作用:对数组的元素进行排序。
语法:array arr.sort([compareFunction]);
参数:compareFunction可选。用来指定按某种顺序进行排列的函数。如果省略,元素按照转换为的字符串的诸个字符的Unicode位点进行排序。
返回值:排序后的数组.

Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

使用js变量作用域遇到的bug

自定义搭建前端脚手架

The above is the detailed content of Detailed explanation of steps to use JS array 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!