数组的添加、删除、排序、与字符串之间的转换、截取、查找、遍历操作总结!!!
2015-07-24 23:07
706 查看
1.添加
(1)插入
(2)属性length
(3)push()
(4)splice(),详情见下
(5)unshift(item,item,...):前端插入,返回长度
2.删除:
(1)delete:数组长度不变,被删除的元素用“”代替
(2)属性length (经测数组每个项都必须是相同的类型才可行)
(3)shift():取得第一项,数组长度减1
(4)pop() 返回被删除的值,取得最后一项
(5)splice(),详情见下
3.以数组方式访问字符串
4.排序
(1)反转 reverse()
(2)升序:sort()
(3)降序:sort(arg)
或着
5.转换为字符串:
(1)join()
(2)toString()和toLocaleString()
(3)reduce(function(){})
6.字符串转换为数组:split()
7.截取 slice(start,end) : 返回数组片段,原数组不变
8.截取,插入,删除 splice(start,delNum,item,item,....) : 返回数组片段,原数组改变
9.项查找:indexOf()和lastIndexOf()
10.数组值的求和reduce()和reduceRight()
11.迭代,遍历
(1)forEach():没有返回值
(2)every():有返回值,所有项都返回true则为true
(3)some():有返回值,有意向返回true则为true
(4)map():返回每次调用的结果组成的数组
(5)filter():返回为true的项
噗 = = 雷死爹了
(1)插入
> var a=[11,22,33] undefined > a [ 11, 22, 33 ] > a[3]="";a[6]="abc";a[] 'abc' > a [ 11, 22, 33, '', , , 'abc' ]
(2)属性length
(3)push()
> a [ 111, 222, 333, 'abc' ] > a.push('ddd') 5
(4)splice(),详情见下
(5)unshift(item,item,...):前端插入,返回长度
> a [ 'b', 'c', 'd', 1, 0 ] > a.unshift("new1","new2") 7 > a [ 'new1', 'new2', 'b', 'c', 'd', 1, 0 ](6)concat(item,item,item):在复制数组的基础上添加项,原数组不变(slice也不影响原数组)
<pre name="code" class="html">> a [ 7, 6, 2, 1, 0 ]> var b=a.concat('aaa','bbb');undefined> b[ 7, 6, 2, 1, 0, 'aaa', 'bbb' ]> a[ 7, 6, 2, 1, 0 ]
2.删除:
(1)delete:数组长度不变,被删除的元素用“”代替
> a [ 11, 22, 33, '', , , 'abc' ] > delete a[0] true > a [ , 22, 33, '', , , 'abc' ]
(2)属性length (经测数组每个项都必须是相同的类型才可行)
> var a=[0,1,2] undefined > a.length 3 > a.length=2 2 > a [ 0, 1 ]
(3)shift():取得第一项,数组长度减1
> a [ 'aaa', 'b', 'c', 'd', 1, 0 ] > a.shift() 'aaa' > a
(4)pop() 返回被删除的值,取得最后一项
(5)splice(),详情见下
> a [ 111, 222, , 'abc', 'ddd' ] > a.pop() 'ddd' > a [ 111, 222, , 'abc' ]
3.以数组方式访问字符串
> var c="hello" > c[1] 'e'
4.排序
(1)反转 reverse()
> var a=[0,4,6,2]; undefined > a.reverse() [ 2, 6, 4, 0 ]
(2)升序:sort()
(3)降序:sort(arg)
> function compare(a,b){return a>b?-1:(a<b?1:0);} undefined > var a=[0,6,8,2,1]; undefined > a.sort(compare) [ 8, 6, 2, 1, 0 ]
或着
> function compare(a,b){return b-a;} undefined > a.sort(compare) [ 8, 6, 2, 1, 0 ]
5.转换为字符串:
(1)join()
> a [ 8, 6, 2, 1, 0 ] > a.join("-") '8-6-2-1-0'
(2)toString()和toLocaleString()
> a.toString(); 'aaa,b,c,d,1,0' > a.toLocaleString(); 'aaa,b,c,d,1,0'
(3)reduce(function(){})
> var a=[1,2,'aa','cc']; undefined > a.reduce(function(pre,cur,index,array){return pre+cur;}); '3aacc' > var b=a.reduce(function(pre,cur){return pre+cur;}); undefined > typeof b 'string'
6.字符串转换为数组:split()
> var str='hello world'; undefined > str.split('l'); [ 'he', '', 'o wor', 'd' ] > str.split(''); [ 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' ] > str.split(' '); [ 'hello', 'world' ]
7.截取 slice(start,end) : 返回数组片段,原数组不变
> a [ 8, 6, 2, 1, 0 ] > a.slice(2,4) [ 2, 1 ] > a.slice(2,40) [ 2, 1, 0 ] > a [ 8, 6, 2, 1, 0 ] > a.slice(-40,40) [ 8, 6, 2, 1, 0 ] > a.slice(-1,3) [] > a.slice(0,3) [ 8, 6, 2 ]
8.截取,插入,删除 splice(start,delNum,item,item,....) : 返回数组片段,原数组改变
> a [ 8, 6, 2, 1, 0 ] > a.splice(0,3,'aaa','b') [ 8, 6, 2 ] > a [ 'aaa', 'b', 1, 0 ]
<p class="p1"></p><p class="p1"><span class="s1">> a.splice(2,0,'c','d')</span></p><p class="p1"><span class="s1">[]</span></p><p class="p1"><span class="s1">> a</span></p><p class="p1"><span class="s1">[ </span><span class="s2">'aaa'</span><span class="s1">, </span><span class="s2">'b'</span><span class="s1">, </span><span class="s2">'c'</span><span class="s1">, </span><span class="s2">'d'</span><span class="s1">, </span><span class="s3">1</span><span class="s1">, </span><span class="s3">0</span><span class="s1"> ]</span></p>
9.项查找:indexOf()和lastIndexOf()
> var a=[1,2,3,4,5,6] undefined > a.indexOf(1) 0 > a.lastIndexOf(1) 0
10.数组值的求和reduce()和reduceRight()
> var a=[1,2,'aa','cc']; undefined > a.reduce(function(pre,cur,index,array){return pre+cur;}); '3aacc' > a.reduce(function(pre,cur){return pre+cur;}); '3aacc'
> var a=[1,2,4,4]; > a.reduce(function(pre,cur,index,array){return pre+cur;}); 11
11.迭代,遍历
(1)forEach():没有返回值
(2)every():有返回值,所有项都返回true则为true
(3)some():有返回值,有意向返回true则为true
(4)map():返回每次调用的结果组成的数组
(5)filter():返回为true的项
噗 = = 雷死爹了
相关文章推荐
- LintCode - Merge Two Sorted List
- ssh方式访问oschina仓库
- typedef和define详细区别
- 重建二叉树(根据 前序 和 中序二叉树)
- Android事件分发机制完全解析,带你从源码的角度彻底理解(下)
- 记录我的VHDL之路(三)
- mysql中文乱码,修改配置为utf-8
- 暑假规划及小学期总结
- Android事件分发机制完全解析,带你从源码的角度彻底理解(上)
- 修改UITextfield的Placeholder字体的颜色
- C++ 把输出结果写入文件/从文件中读取数据
- NoSQL 数据建模技术
- Android开发环境配置问题
- 栈与队列
- 求任意一个数字的各位数字之和
- python的数据类型
- CodeSmith 代码具体操作步骤
- 大数乘法
- Oracle EBS(ERP)行业分工(写给猎头)
- hdoj 1002 大数加法问题