原文链接:http://www.jianshu.com/p/d6094d40855f
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
在W3C标准中有很多Array实例对象的方法,它比上一篇讲到的《与数组相关的全局方法》更多,而且在工作中也更实用。同样的,我在这里把一些常用的方法小结一下,多加练习。
常见的Array实例对象的方法有valueOf(), toString(), push(), pop(), join(), concat(), shift(), unshift(), reverse(), slice(), splice(), sort() 等等,下面就一一来介绍一下吧。
1、valueOf方法,toString方法
valueOf方法返回数组本身。
![](http://upload-images.jianshu.io/upload_images/1181204-9adec2edf0d23a30.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
toString 方法返回数组的字符串形式。
![](http://upload-images.jianshu.io/upload_images/1181204-c84e049bc3dc44c5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2、push方法,pop方法
push方法用于在数组的末端添加一个或多个元素,并返回添加后的数组的长度。
![](http://upload-images.jianshu.io/upload_images/1181204-7a689a3f8e51bce1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
如果需要合并两个数组,可以这样写。
![](http://upload-images.jianshu.io/upload_images/1181204-5186d456133ec638.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
难点:push方法还可以用于向对象添加元素,添加后的对象变成“类似数组的”对象,即新加入元素的键对应数组的索引,并且对象有一个length属性。
![](http://upload-images.jianshu.io/upload_images/1181204-f87dc375d12a78d6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
pop方法用于删除数组的最后一个元素,并返回该元素。
![](http://upload-images.jianshu.io/upload_images/1181204-39c46e63bd97384f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3、join方法,concat方法
join方法以参数作为分隔符,将所有数组成员组成一个字符串返回。如果不提供参数,默认用逗号分隔。
![](http://upload-images.jianshu.io/upload_images/1181204-1e544f6ac0019cc3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
通过函数的call方法,join方法(即Array.prototype.join)也可以用于字符串。
![](http://upload-images.jianshu.io/upload_images/1181204-51d69cd60b638623.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
concat方法用于多个数组的合并。它将新数组的成员,添加到原数组的尾部,然后返回一个新数组。
![](http://upload-images.jianshu.io/upload_images/1181204-34306087135de7d8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
这里需要注意的是通过concat()返回一个新数组后不会改变原数组的值,而通过push()方法连接则会改变原数组的值,例如:
![](http://upload-images.jianshu.io/upload_images/1181204-ae5400ac6f05e03f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
concat方法也可以用于将对象合并为数组,但是必须借助call方法。
![](http://upload-images.jianshu.io/upload_images/1181204-5f3866e48c6aedc4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
4、shift方法,unshift方法
shift方法用于删除数组的第一个元素,并返回该元素。
![](http://upload-images.jianshu.io/upload_images/1181204-fefa1947493241bf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
unshift方法用于在数组的第一个位置添加元素,并返回添加新元素后的数组长度。
![](http://upload-images.jianshu.io/upload_images/1181204-f73d1573b1ee9569.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
5、reverse方法
reverse方法用于颠倒数组中元素的顺序,使用这个方法以后,返回改变后的原数组。
![](http://upload-images.jianshu.io/upload_images/1181204-59bafa221f552cbe.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
6、slice方法
slice方法用于提取原数组的一部分,返回一个新数组,原数组不变。
它的第一个参数为起始位置(从0开始),第二个参数为终止位置(但该位置的元素本身不包括在内)。如果省略第二个参数,则一直返回到原数组的最后一个成员。
![](http://upload-images.jianshu.io/upload_images/1181204-c86fb668a215c796.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
上面代码表示,如果slice方法的参数是负数,则从尾部开始选择的成员个数;如果参数值大于数组成员的个数,或者第二个参数小于第一个参数,则返回空数组。
slice方法的一个重要应用,是将类似数组的对象转为真正的数组。
![](http://upload-images.jianshu.io/upload_images/1181204-8194d585054cc1b2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
7、splice方法
splice方法用于删除原数组的一部分成员,并可以在被删除的位置添加入新的数组成员。它的返回值是被删除的元素。该方法会改变原数组。
splice的第一个参数是删除的起始位置,第二个参数是被删除的元素个数。如果后面还有更多的参数,则表示这些就是要被插入数组的新元素。
![](http://upload-images.jianshu.io/upload_images/1181204-d417dccd2406c4d7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
上面代码从原数组位置4开始,删除了两个数组成员。
![](http://upload-images.jianshu.io/upload_images/1181204-f50f841edad62195.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
上面代码除了删除成员,还插入了两个新成员。
如果只是单纯地插入元素,splice方法的第二个参数可以设为0。
![](http://upload-images.jianshu.io/upload_images/1181204-cbbbd59c2d932c1e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
如果只提供第一个参数,则实际上等同于将原数组在指定位置拆分成两个数组。
![](http://upload-images.jianshu.io/upload_images/1181204-e3764b1319422326.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
8、sort方法
sort方法对数组成员进行排序,默认是按照字典顺序排序。排序后,原数组将被改变。
![](http://upload-images.jianshu.io/upload_images/1181204-099f02c0eb8be303.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
上面代码的最后两个例子,需要特殊注意。sort方法不是按照大小排序,而是按照对应字符串的字典顺序排序,所以101排在11的前面。
如果想让sort方法按照自定义方式排序,可以传入一个函数作为参数,表示按照自定义方法进行排序。该函数本身又接受两个参数,表示进行比较的两个元素。如果返回值大于0,表示第一个元素排在第二个元素后面;其他情况下,都是第一个元素排在第二个元素前面。
![](http://upload-images.jianshu.io/upload_images/1181204-eebded15b8023ba9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)