1、判断数组是数组的方法 参考 MDN
- Array.isArray()
- Object.prototype.toString.call(obj) === '[object Array]'
2、转化方法
- Array.toString()
var months = ['Jan', 'Feb', 'Mar', 'Apr'];
months.toString(); // "Jan,Feb,Mar,Apr"
3、join
months.join() // 'jan,feb,mar'
months.join(', ') // 'jan, feb, mar'
months.join(' + ')// 'jan + feb + mar'
months.join('')//'janfebmar'
tip:
如果数组中的某一项的值是 null 或者 undefined,
那么该值在 join()、toLocale-String()、toString() 和 valueOf() 方法返回的结果中以空字符串表示
4、 栈方法
- pop 删除数组的最后一项操作原数组,返回删除项
- push 在数组最后添加一个新元素
- shift 删除数组的第一项
- unshift 数组的前面增加新元素
5、reverse 反转数组项
操作原数组,返回数组
var arr = ['one', 'two', 'three']
var b = arr.reverse;
console.log(a) //['three', 'two', 'one']
console.log(b) //['three', 'two', 'one']
6、sort数组排序
7、concat合并数组
8、slice切割数组
9、splice 删除、插入一个元素
参数(a,b,c) a:起始位置 b:要删除的个数 c:替换和增加
var arr = ['a', 'b', 'c']
arr.splice(1,0,'d') //['a', 'd', 'b', 'c']
arr.splice(2,1) //['a', 'd', 'c']
arr.splice(-1,0,'drum')// ['a', 'd', 'drum', 'c']
10、indexOf() lastIndexOf()
indexOf 和 lastIndexOf 都接受两个参数:查找的值、查找起始位置 不存在,返回 -1 ;存在,返回位置。indexOf 是从前往后查找, lastIndexOf 是从后往前查找。
11、every
对数组每一项都运行给定的函数,每一项返回true,则返回true
function isBigEnough(element, index, array) {
return element < 10;
}
[2, 5, 8, 3, 4].every(isBigEnough); // true
11、some
对数组的每一项都运行给定的函数,任意一项返回 ture,则返回 true
function isBiggerThan10(element, index, array) {
return element > 10;
}
[2, 5, 8, 1, 4].some(isBiggerThan10); // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true
12、filter
对数组的每一项都运行给定的函数,返回 结果为 ture 的项组成的数组
var words = ["spray", "limit", "elite", "exuberant", "destruction", "present", "happy"];
var longWords = words.filter(function(word){
return word.length > 6;
});
// Filtered array longWords is ["exuberant", "destruction", "present"]
13、map
对数组的每一项都运行给定的函数,返回每次函数调用的结果组成一个新数组
14、forEach 数组遍历
15、reduce reduceRight
小结
队列方法和栈方法操作的都是原数组,增加数组元素的时候,返回值是数组长度;删除数组元素的时候,返回值是被删除的元素。
哪些方法改变了原数组?
栈方法:push、pop
队列方法:shift、unshift
重排序方法:reverse、sort
操作方法:splice
哪些方法返回的是数组?
重排序方法:reverse、sort
操作方法:splice、slice、concat
迭代方法中:filter、map
16、ECMAScript 6.0 新增的方法
- from 将类数组转化为数组
- of 这个方法的主要目的,是弥补数组构造函数 Array() 的不足。因为参数个数的不同,会导致 Array() 的行为有差异。
Array() // []
Array(3) // [, , ,]
Array(3, 11, 8) // [3, 11, 8]
Array.of(7); // [7]
Array.of(1, 2, 3); // [1, 2, 3]
- copyWithin
将指定位置的元素复制到其他位置(会覆盖原有元素),返回当前数组。该方法会修改当前数组。
它接受三个参数。
target(必需):从该位置开始替换数据。
start(可选):从该位置开始读取数据,默认为0。如果为负值,表示倒数。
end(可选):到该位置前停止读取数据,默认等于数组长度。如果为负值,表示倒数。
[1, 2, 3, 4, 5].copyWithin(-2);
// [1, 2, 3, 1, 2]
[1, 2, 3, 4, 5].copyWithin(0, 3);
// [4, 5, 3, 4, 5]
[1, 2, 3, 4, 5].copyWithin(0, 3, 4);
// [4, 2, 3, 4, 5]
[1, 2, 3, 4, 5].copyWithin(-2, -3, -1);
// [1, 2, 3, 3, 4]
- fill
使用给定值,填充一个数组。
会抹除数组原有的元素
还可以接受第二个和第三个参数,用于指定填充的起始位置和结束位置。
var numbers = [1, 2, 3]
numbers.fill(1);// results in [1, 1, 1]
['a', 'b', 'c'].fill(7, 1, 2)// ['a', 7, 'c']
- find
找出第一个符合条件的数组元素,参数是一个回调函数,所有数组元素依次执行该回调函数,
直到找出第一个返回值为 true 的元素,然后返回该元素。
如果没有符合条件的元素,则返回 undefined。回调函数可以接受三个参数,依次为当前的值、当前的位置和原数组。
[1, 5, 10, 15].find(function(value, index, arr) {
return value > 9;
}) // 10
[1, 5, 2, 3].find(function(value, index, arr) {
return value > 9;
}) // undefined
- findIndex
findIndex 方法的用法与 find 方法非常类似,返回第一个符合条件的数组元素的位置,如果所有元素都不符合条件,则返回 -1。
[1, 5, 10, 15].findIndex(function(value, index, arr) {
return value > 9;
}) // 2
- ES6 提供三个新的方法—— entries(),keys()和 values() —— 用于遍历数组。它们都返回一个遍历器对象,可以用 for…of 循环进行遍历,唯一的区别是 keys() 是对键名的遍历、values() 是对键值的遍历,entries() 是对键值对的遍历。
for (let index of ['a', 'b'].keys()) {
console.log(index);
}
// 0
// 1
for (let elem of ['a', 'b'].values()) {
console.log(elem);
}
// 'a'
// 'b'
for (let [index, elem] of ['a', 'b'].entries()) {
console.log(index, elem);
}
// 0 "a"
// 1 "b"
1、 entries
var a = ['a', 'b', 'c'];
var iterator = a.entries();
console.log(iterator.next().value); // [0, 'a']
console.log(iterator.next().value); // [1, 'b']
console.log(iterator.next().value); // [2, 'c']
2、 keys
var arr = ['a', 'b', 'c'];
var iterator = arr.keys();
console.log(iterator.next()); // { value: 0, done: false }
console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: undefined, done: true }
3、values
var a = ['w', 'y', 'k', 'o', 'p'];
var iterator = a.values();
console.log(iterator.next().value); // w
console.log(iterator.next().value); // y
console.log(iterator.next().value); // k
console.log(iterator.next().value); // o
console.log(iterator.next().value); // p
ECMAScript 7.0 新增的方法
- includes
判断数组中是否存在该元素
参数:查找的值、起始位置
可以替换 ES5 时代的 indexOf 判断方式
var a = [1, 2, 3];
a.includes(2); // true
a.includes(4); // false