一、join() 将数组的全部元素链接成一个字符串数组
Array.join()方法将数组中全部元素都转化为字符串并链接在一块儿,返回最后生成的字符串。若是不指定分隔符,默认使用逗号。函数
1 var a = [1,2,3]; 2 a.join(); // 1,2,3 3 a.join(" "); // 1 2 3 4 5 var b = new Array(10); 6 b.join("-"); // => '---------':9个连字号组成的字符串
二、sort() 给数组元素排序spa
一维数组:code
从大到小排序:blog
arr.sort(function(a, b) { return b - a; });
从小到大排序:排序
arr.sort(function(a, b) { return a - b; });
注: 二维数组排序three
arr.sort(function(a, b) { return b[1] - a[1]; });
三、concat() 链接两个数组并返回一个新的数组rem
var array = ['first', 'second', 'three', 'fourth']; array = array.concat('a', 'b', 'c'); console.log(array); // ["first", "second", "three", "fourth", "a", "b", "c"]
四、push() 在数组末尾添加一个或多个元素,并返回数组操做后的长度字符串
var array = ['first', 'second', 'three', 'fourth']; array.push('five'); console.log(array); // ["first","second","three","fourth","five"]
五、pop() 从数组移出最后一个元素,并返回该元素回调函数
var myArray = new Array("1", "2", "3"); myArray.pop(); console.log(myArray); // ["1","2"]
六、shift() 从数组移出第一个元素,并返回该元素
var myArray = new Array("1", "2", "3"); myArray.shift(); console.log(myArray); // ["2","3"]
七、unshift() 在数组开头添加一个或多个元素,并返回数组的新长度
var myArray = new Array("1", "2", "3"); myArray.unshift("-1", "0"); console.log(myArray); // ["-1", "0","1","2","3"]
八、slice(start_index, upto_index) 从数组提取一个片断,并做为一个新数组返回
var myArray = new Array ("a", "b", "c", "d", "e"); myArray = myArray.slice(1, 4); console.log(myArray); // ["b", "c", "d"]
九、reverse() 颠倒数组元素的顺序:第一个变成最后一个,最后一个变成第一个
var myArray = new Array ("1", "2", "3"); myArray.reverse(); console.log(myArray); // ["3","2","1"]
十、splice(index, count_to_remove,addElement1,addElement2...) 从数组移出一些元素,(可选)并替换它们
var myArray = new Array ("1", "2", "3", "4", "5"); myArray.splice(1, 3, "a", "b", "c", "d"); console.log(myArray); // ["1", "a", "b", "c", "d", "5"]
十一、map() 在数组的每一个单元项上执行callback函数,并把返回包含回调函数返回值的新数组。
var a1 = ['a', 'b', 'c']; var a2 = a1.map(function(item){ return item.toUpperCase(); }); console.log(a2); // ["A","B","C"]
十二、filter() 返回一个包含全部在回调函数上返回为true的元素的新数组。
var a3 = ['a', 10, 'b', 20, 'c', 30]; var a4 = a3.filter(function(item){ return typeof item == 'number'; }); console.log(a4); // [10,20,30]