ES6数组高级API
-
遍历数组
-
通过for of循环遍历
-
格式:
let arr = [1, 3, 5, 7, 9]; for (let value of arr) { console.log(value); }
-
-
通过forEach方法
-
格式:
let arr = [1, 3, 5, 7, 9]; //第一个参数当前遍历到的元素,第二个参数为当前遍历到的索引,第三个参数为当前被遍历的数组 arr.forEach(function (currentValue, currentIndex, currentArray) { console.log(currentValue, currentIndex, currentArray); })
-
-
自定义forEach方法
-
格式:
let arr = [1, 3, 5, 7, 9]; Array.prototype.myForEach = function (fn) { 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方法:找到返回索引,找不到返回-1
-
格式:
let arr = [1, 3, 5, 7, 9]; let index = arr.findIndex(function (currentValue, currentIndex, currentArray) { if (currentValue === 5) return true; }) console.log(index); //打印 2
-
自定义:
let arr = [1, 3, 5, 7, 9]; Array.prototype.myFindIndex = function (fn) { for (let i = 0; i < this.length; i++) { if (fn(this[i], i, this)) return i; } } let index = arr.myFindIndex(function (currentValue, currentIndex, currentArray) { if (currentValue === 5) return true; }) console.log(index);
-
-
find方法:找到返回元素值,找不到返回undefined
-
格式:
let value = arr.find(function (currentValue, currentIndex, currentArray) { if (currentValue === 5) return true; }) console.log(value); //打印 5
-
自定义:
let arr = [1, 3, 5, 7, 9]; Array.prototype.myFind = function (fn) { for (let i = 0; i < this.length; i++) { if (fn(this[i], i, this)) return this[i]; } } let value = arr.myFind(function (currentValue, currentIndex, currentArray) { if (currentValue === 5) return true; }) console.log(value);
-
-
数组过滤
-
filter方法:将满足条件的元素添加到一个新数组中并返回新数组
-
格式:
let arr = [1, 2, 3, 4, 5, 6]; let newArray = arr.filter(function (currentValue, currentIndex, currentArray) { if (currentValue % 2 === 0) { return true; } }) console.log(newArray); //打印[2,4,6]
-
自定义:
let arr = [1, 2, 3, 4, 5, 6]; Array.prototype.myFilter = function (fn) { let arr = []; for (let i = 0; i < this.length; i++) { if (fn(this[i], i, this)) { arr.push(this[i]); } } return arr; } let newArray = arr.myFilter(function (currentValue, currentIndex, currentArray) { if (currentValue % 2 === 0) { return true; } }) console.log(newArray); //打印[2,4,6]
-
-
数组映射
-
map方法:将满足条件的元素一 一映射到一个新数组(没有的则为undefined)中并返回新数组
-
格式:
let arr = [1, 2, 3, 4, 5, 6]; let newArray = arr.map(function (currentValue, currentIndex, currentArray) { if (currentValue % 2 === 0) { return currentValue; } }) console.log(newArray); //打印[undefined,2,undefined,4,undefined,6]
-
自定义:
let arr = [1, 2, 3, 4, 5, 6]; Array.prototype.myMap = function (fn) { let newArr = new Array(arr.length); for (let i = 0; i < this.length; i++) { if (fn(this[i], i, this)) { newArr[i] = this[i]; } } return newArr; } let newArray = arr.myMap(function (currentValue, currentIndex, currentArray) { if (currentValue % 2 === 0) { return currentValue; } }) console.log(newArray);
-
-
数组遍历删除元素(从后面开始删除)
-
delete方法:删除数组中元素,不会改变length值,也不会删除元素后使得后面元素前移
-
格式:
let arr = [1, 2, 3, 4, 5, 6]; for (let i = 0; i < arr.length; i++) { delete arr[i]; } console.log(arr); //Array(6)
-
-
数组排序
-
sort方法:对数组进行自定义排序
-
格式:
let arr1 = ["c", "a", "b"]; let arr2 = [3, 5, 1, 4, 2]; let arr3 = ["1234", "6", "321", "4321"]; let arr4 = [ {name: "nhw", age: 18}, {name: "zbj", age: 14}, {name: "xxx", age: 15}, {name: "abc", age: 18} ]; //a与b比较ASCII码大小 //升序 arr1.sort(function (a, b) { if (a < b) return 1; //若返回大于0,则a会在b之前 else if (a > b) return -1; //若返回小于0,则b会在a之前 else return 0; //若返回等于0,则不变 }) arr2.sort(function (a, b) { return a - b; //return b - a; //数值降序 }) arr3.sort(function (a, b) { return a.length - b.length; //return b.length - a.length; //字符串长度降序 }) arr4.sort(function (a, b) { return a.age - b.age; //return b.age - a.age; //对象中年龄降序 })
-