NDM数组操作方法地址:Array - JavaScript | MDN
常用的数组方法
push
可以将一个或者更多的参数添加再数组的尾部;返回添加后的数组长度,原数组发生改变
pop
从数组尾部删除一个元素,返回这个被删除的元素,数组发生改变
unshift
将一个或者更多的参数添加到数组的头部;返回添加后的数组长度,原数组发生改变
shift
从数组头部删除一个元素;返回被删除的元素,原数组发生改变
reverse
用于颠倒数组中元素的排序
sort
用于堆数组元素进行排序
join
用于把数组中的所有元素转换成一个字符串,元素是通过指定的分隔符进行切割
filter
过滤出一些符号条件的元素,返回一个新数组
forEach
按升序为数组中含有效值的每一项执行一次
callback
函数,那些已删除或者未初始化的项将被跳过
map
按照原始数组元素顺序依次处理元素,数组中的元素为原始数组元素调用函数处理的后值;返回一个新数组
reduce
对数组中的每个元素执行一个自定义的累计器,将其结果汇总为单个返回值
reduce高级用法:
slice
array.slice(start, end)
1、如果不传参数,会返回原数组;如果一个参数,从该参数表示的索引开始截取,直至数组结束,返回这个截取数组,
2、原数组不变;两个参数,从第一个参数对应的索引开始截取,到第二个参数对应的索引结束,但包括第二个参数对应的索引上的值,
splice
array.splice(index, howMany, item1, ..., itemX)
1、没有参数,返回空数组,原数组不变;
2、一个参数,从该参数表示的索引位开始截取,直至数组结束,返回截取的数组,原数组改变;
3、两个参数,第一个参数表示开始截取的索引位,第二个参数表示截取的长度,返回截取的 数组,原数组改变;
4、三个或者更多参数,第三个及以后的参数表示要从截取位插入的值。
concat
属于字符串的方法,数组也可以使用,接收一个或多个参数,将参数中的值放到操作的数组后边,返回拼接的数组,原数组不变。如果参数是一个数组,则先把值提取出来再操作。
indexOf
属于字符串的方法,数组也可以使用,该方法可以返回某个指定的字符串值再字符串中首次出现的位置;若一个参数,返回这个参数在数组里面的索引值,如果参数不在操作的数组中,则返回-1
some
array.some(function (currentValue, index, arr), thisValue )
检查数组中是否含有某一个值,返回一个布尔值,如果数组中任意一个元素满足给定条件,结果为true,否则为false
every
array.every(function (currentValue, index, arr), thisValue )
用于检测数组所有元素是否都符合指定条件(通过函数提供),返回一个布尔值,结果为 true或false。
ES6新增数组方法
NDM数组操作方法地址:Array - JavaScript | MDN
Array.from
将类数组对象或可迭代对象转化为数组,比如arguments,js选择器找到dom集合和对象模拟的数组。
Array.of
数组创建,将参数中所有值作为元素形成数组,如果参数为空,则返回一个空数组
find
查找数组中符合条件的元素,若有多个符合条件的元素,则返回第一个元素。 查找数组中符合条件的元素索引,若有多个符合条件的元素,则返回第一个元素索引
includes
检测数组中是否包含一个值
注意:与 Set 和 Map 的 has 方法区分;Set 的 has 方法用于查找值;Map 的 has 方法用于查找键名
fill
将一定范围索引的数组元素内容填充为单个指定的值
entries
遍历键值对
for in
一般循环遍历的都是对象的属性,遍历对象本身的所有可枚举属性,以及对象从其构造函数原型中继承的属性key会变成字符串类型
for of
修复了ES5中for in的不足,允许遍历 Arrays(数组)、Strings(字符串)、Maps(映射)、Sets(集合)等可迭代的数据结构
for of 支持return, 只能遍历数组不能遍历对象(遍历对象需要通过和Object.keys()搭配使用)
// 1) 循环一个数组 let arr = ['A', 'B', 'C']; for(let item of arr){ console.log(item); } // 2) 循环一个字符串 let iterable = "abc"; for (let value of iterable) { console.log(value); } // 3) 循环一个拥有enumerable属性的对象 let someObject = { a:'a1', b:'b2' }; for (let key of Object.keys(someObject)) { console.log(key + ": " + someObject[key]); }
实现数组API方法
// forEach
Array.prototype.myForEach = function (fn) {
// this === [1, 3, 5, 7, 9]
for(let i = 0; i < this.length; i++){
fn(this[i], i, this);
}
};
arr.myForEach(function (currentValue, currentIndex, currentArray) {
console.log(currentValue, currentIndex, currentArray);
});
// findIndex
Array.prototype.myFindIndex = function (fn) {
for(let i = 0; i < this.length; i++){
let result = fn(this[i], i, this);
if(result !== undefined){
return result;
}
}
return undefined;
}
let index = arr.myFindIndex(function (currentValue, currentIndex, currentArray) {
// console.log(currentValue, currentIndex, currentArray);
if(currentValue === 10){
return true;
}
});
console.log(index);
// filter
Array.prototype.myFilter = function (fn) {
let newArray = [];
for(let i = 0; i < this.length; i++){
let result = fn(this[i], i, this);
if(result){
newArray.push(this[i]);
}
}
return newArray;
}
// map
Array.prototype.myMap = function (fn) {
let newArray = new Array(this.length);
newArray.fill(undefined);
for(let i = 0; i < this.length; i++){
let result = fn(this[i], i, this);
if(result !== undefined){
newArray[i] = result;
}
}
return newArray;
}