Home > Web Front-end > JS Tutorial > Practical skills for javascript array operations_javascript skills

Practical skills for javascript array operations_javascript skills

WBOY
Release: 2016-05-16 18:46:40
Original
1014 people have browsed it

1. concat method
[Function] Combine multiple arrays. This method does not change the existing array. It only returns a copy of the combined array.
[Syntax] arrayObj.concat(array1,array2,...)
[Example]


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

2. join Method,

[Function] Convert the original array object into a string, which can be concatenated with the specified delimiter.
[Syntax] arrayObj.join(separator), separator is the separator, and the default is ",".
[Example]

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]
3. pop , shift method

[Function] The pop method deletes and returns the last element in the array, and at the same time, the length of the array changes. In contrast, the shift() method deletes and returns the first element of the array.
[Syntax] arrayObj.pop|shift()
[Example]
[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute ]

4. push , unshift method

[Function] Add one or more elements to the end of the array and return the new length. At the same time, the length of the array will be changed, corresponding to the unshift method. Note that the return value of the method is not the new array, but the length of the new array.
[Syntax] arrayObj.push|unshift(ele1,ele2,ele3,...)

[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]


5. reverse Method
[Function] Reverse the order of elements in the array. [Example]

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]


6. slice Method
[Syntax] arrayObj.slice(startposition,endposition), the first parameter is required, the second parameter is optional, if not written, the default is from startposition to the last element. Note that the element containing the startposition position does not include the endposition.
[Example]
[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute ]
7、sort方法
[作用] 用来对数组元素进行排序。
[语法] arrayObj.sort(sortRule())。参数可选,代表排序的方法。特别说明,sort排序有几个基本原则,第一,默认按照字母顺序排列;第二,大写字母排在小写字母之前。基与此,对于数字排序,需要自己写排序方法。
[实例]

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

8、splice方法
[作用] 向数组删除并加入新的元素。
[语法] arrayObj.splice(index,number,ele1,ele2...)。index为索引位置,必须为数字,代表从哪个位置插入或者删除元素。number,代表从index位置往后删除的元素个数,如果为“0”则表示在index插入新元素,不删除往后的数组元素。(说得太混了~~~直接看代码实例)
[实例]

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

9、toString方法
[作用] 使用率非常高的方法,直接将数组类型对象转换成字符串类型对象。
[语法] arrayObj.toString()
[实例]

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

注意:新的字符串是原数组连接而成,但是中间有“,”分隔,如想去掉“,”,可以这样写:

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
Related labels:
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