toString() 方法
数组的toString方法返回数组的字符串形式
let arr = ['it', 'he', 'she'];
arr.toString(); // "it,he,she"
let arr = ['it', 'he', 'she', ['1', '2', '3']];
arr.toString(); // "it,he,she,1,2,3"
push(), pop()方法会改变原数组
push 方法在数组的末尾添加一个或多个元素,并且返回添加后的数组长度
let arr = [];
arr.push(1); // 1
arr.push(true, {}); // 3
arr // [1, 'a', true, {}]
pop方法用于删除数组的最后一个元素,并返回该元素。如果对空数组进行pop操作返回undefined
let arr = [1, 2, 3];
arr.pop(); // 'c'
arr // ['a', 'b']
[].pop(); // undefined
shift(), unshift() 方法会改变原数组
shift方法用于删除数组的第一个元素,并返回该元素。 使用shift可以清空函数
let a = ['a', 'b', 'c'];
a.shift(); // 'a'
a // ['b', 'c']
while(item = list.shift()) {
console.log(item);
}
unshift方法用于在数组的第一个位置添加元素,可以传入多个参数,并返回添加新元素后的数组长度。
let arr = [ 'c', 'd' ];
arr.unshift('a', 'b') // 4
arr // [ 'a', 'b', 'c', 'd' ]
join() 方法以指定参数作为分隔符,将所有数组成员连接为一个字符串返回。如果不提供参数,默认用逗号分隔。
let a = [1, 2, 3, 4];
a.join(' ') // '1 2 3 4'
a.join(' | ') // "1 | 2 | 3 | 4"
a.join() // "1,2,3,4"
//如果数组中有undefined、null或者空位,会被转换成空字符串。
let arr = ['b', undefined, null, , 'a'];
arr.join('-'); // 'b----a'
concat() 合并数组
concat该方法可以将其他数组、变量或者其他对象合并到此数组中,然后返回一个新数组。
let a = ['hello'];
let b = ['world'];
let c = 100;
let d = {n: "name"};
a.concat(b); // ["hello", "world"];
a.concat(b, c); // ["hello", "world", 100];
a.concat(d); // ["hello", {n: name}]
注意:如果数组成员包括对象concat方法返回数组的一个浅拷贝(新数组所拷贝的是对象的引用)
let obj = {a: 1};
let oldArr = [obj];
let newArr = oldArr.concat();
obj.a = 10;
newArr[0].a // 2
reverse() 翻转数组
reverse方法用于将数组前后对调,此方法会改变原数组
let a = ['a', 'b', 'c'];
a.reverse();
a // ['c', 'b', 'a']
slice() 原数组不变
slice方法用于提取目标数组的一部分,返回一个新数组。
arr.slice(start, end);
start起始下标,默认从0开始 end结束下标,默认到最后元素
let a = ['a', 'b', 'c'];
a.slice(1) // ["b", "c"]
a.slice(2, 6) // ["c"]
a.slice() // ["a", "b", "c"]
// 参数可以是负数,表示倒数计算的位置
let a = ['a', 'b', 'c'];
a.slice(-2) // ["b", "c"] 倒数第二个开始
splice() 该方法会改变原数组
splice方法用于删除原数组的一部分成员,并可以在删除的位置添加新的数组成员,返回值是被删除的元素。
arr.splice(start, count, addElement1, addElement2, …);
start 起始位置 count 删除个数 add… 新添加参数
let a = ['a', 'b', 'c', 'd', 'e', 'f'];
a.splice(4, 2) // ["e", "f"]
a // ["a", "b", "c", "d"]
let a = ['a', 'b', 'c', 'd', 'e', 'f'];
a.splice(2);
a // ['a', 'b']
添加
let a = [1, 1, 1];
a.splice(1, 0, 2) // []
a // [1, 2, 1, 1]
sort() 改变原数组
方法对数组成员进行排序,默认是按照字典顺序排序。
let a = ['d', 'c', 'b', 'a'];
a.sort()
a // ['a', 'b', 'c', 'd']
因为排序按照字典顺序,所以当对数字元素排序时
let a = [10111, 1101, 111];
a.sort((a, b) => {
return a - b;
});
a // [111, 1101, 10111]
forEach()
对数组的所有成员依次执行参数函数,forEach方法不返回值,只用来操作数据,如果遇到数组空位,会直接跳过
[1, 2].forEach((element, index, array) => {
console.log('[' + index + '] = ' + element);
});
// [0] = 1
// [1] = 2
[1, , 2].forEach((element, index, array) {
console.log('[' + index + '] = ' + element);
});
// [0] = 1
// [1] = 2
// forEach方法也可以接受第二个参数,绑定参数函数的this变量。
let out = [];
[1, 2, 3].forEach(function(elem) {
this.push(elem * elem);
}, out);
out // [1, 4, 9]
indexOf(),lastIndexOf()
indexOf方法返回给定元素在数组中第一次出现的位置,如果没有出现则返回-1。
let a = ['a', 'b', 'c'];
a.indexOf('b') // 1
a.indexOf('y') // -1
indexOf方法还可以接受第二个参数,表示搜索的开始位置。
['a', 'b', 'c'].indexOf('a', 1) // -1 从第二个开始 所以没有搜索到
lastIndexOf方法返回给定元素在数组中最后一次出现的位置,如果没有出现则返回-1。
let a = [2, 5, 9, 2];
a.lastIndexOf(2) // 3
a.lastIndexOf(7) // -1
map()函数 遍历每一个成员,执行结果返回一个新数组,原数组不变
let numbers =[1, 2, 3, 4];
numbers.map((elem, index, arr)=> {
return elem * elem;
});
// [1, 4, 9, 16]
// map方法可以接受第二个参数,用来绑定回调函数内部的this变量
let arr = ['a', 'b', 'c'];
[1, 2].map(function (e) {
return this[e];
}, arr)
// ['b', 'c']