1.concat() => 连接两个或多个数组(不改变原数组)
var arr1 = [1,2,3];
var arr2 = ["a","b","c"];
var arr4 = [4,65];
var str = "111";
var arr3 = arr1.concat(arr2,arr4);
var arr5 = arr1.concat(str);
var arr6 = arr1.concat(arr4[0]); =>可以合并字符串
2.join() => 将数组变成字符串(不改变原数组):参数为指定要使用的分隔符。如果省略该参数,则使用逗号作为分隔符。
var arr2 = ["a","b","c"];
var str = arr2.join(); ==> a,b,c
var str = arr2.join(""); ==> abc
var str = arr2.join("&"); ==> a&b&c
3.indexOf => 检索某个元素第一此出现的索引(不改变原数组,返回索引值)
var arr = ["a","b","c","d","e","c"];
var index = arr.indexOf("c"); =>表示数组从索引0的位置开始检索
var index2 = arr.indexOf("c",3); =>表示数组从索引3的位置开始检索
4.slice() => 截取数组中的元素(不改变原数组,返回截取的新数组)
var arr = ["a","b","c","d","e","c"];
var arr1 = arr.slice(); => 不传参数,默认为0,截取整个数组
var arr2 = arr.slice(2); => 截取数组从索引2位置开始的所有元素
var arr3 = arr.slice(2,4); => 截取数组从索引2位置到索引4位置的元素(不包括4)
5.sort() => 对数组中的元素进行排序(改变原数组,返回重新排序后的新数组)
var arr = ["a","d","z","x","e","c"];
var index = arr.sort(); => 不带参数,默认字母a-z排序,数字从小到大排序
var index2 = arr.sort(function(){
a-b:从小到大排序
b-a:从大到小排序
});
6.reverse() => 颠倒数组(改变原数组,返回重新排序后的新数组)
var arr = ["a","b","c","d","e","c"];
var index = arr.reverse()
7.shift() => 删除数组的第一个元素(改变原数组,返回被删除的第一个元素)
var arr = ["a","b","c","d","e","c"];
var index = arr.shift();
8.pop() => 删除数组中的最后一个元素(改变原数组,返回被删除的最后一个元素)
var arr = ["a","b","c","d","e","c"];
var index = arr.pop();
9.unshift() => 添加数组开头的一个或多个元素(改变原数组,返回新数组的length长度)
var arr = ["a","b","c","d","e","c"];
var arr = arr.unshift("xx"); => 向数组开头添加"xx"单个元素
var arr1 = ["a","b","c","d","e","c"];
var arr1 = arr1.unshift("xx","zz"); => 向数组开头添加"xx","zz"两个元素
10.push() => 添加数组末尾的一个或多个元素(改变原数组,返回新数组的length长度)
var arr = ["a","b","c","d","e","c"];
var index = arr.push("xx"); => 向数组末尾添加"xx" 单个元素
var index = arr.push("xx","zz"); => 向数组末尾添加"xx","zz"两个元素
11.splice() => 插入、删除或替换数组中的元素(改变原数组,返回移除元素的新数组)
var arr = ["a","b","c","d","e","c"];
var index = arr.splice(1); =>删除索引从1开始的所有元素
var arr1 = ["a","b","c","d","e","c"];
var index1 = arr1.splice(1,4); =>删除所有从1到4的所有元素(包括4)
var arr2 = ["a","b","c","d","e","c"];
var index2 = arr2.splice(1,0,"zz"); =>从索引位置1开始,删除0个元素,插入"zz"元素
var arr3 = ["a","b","c","d","e","c"];
var index3 = arr3.splice(1,3,"zz","bb"); =>从索引位置1开始,删除3个元素,插入"zz"和"bb"元素