搜索

ES5 新增数组方法 ES6 新增数组方法


发布时间: 2022-11-24 23:43:01    浏览次数:43 次
ES5 新增数组方法

| 对象调用的方法                                              | 作用                         | 返回值   |
| --------------------------------------------------------- | --------------------------- | --------|
| array.forEach(function(item, index, array){})             | 遍历                         | 无      |
| array.map(function(item, index, array){})                 | 遍历&收集返回的项              | 新数组   |
| array.filter(function(item, index, array){ return 条件 })  | 过滤&保留return true的项      | 新数组   |
| array.reduce(function(sum, item, index, array) {}, 0)     | 遍历&累计求和                 | 累计结果 |
| array.every(function(item, index, array){ return 条件})    | 遍历&判断是否都满足条件         | 布尔值   |
| array.some(function(item, index, array){return 条件})      | 遍历&判断是否有某个满足条件      | 布尔值   |
| Array.from(伪数组)                                         | 伪数组转真数组                 | 真数组   |

ES6 新增数组方法

| 对象调用的方法                                               | 作用      | 返回值               |
| ------------------------------------------------------     | -------- | --------------------|
| array.find(function(item, index) { return 条件 })           | 遍历查找  | 找到的项 / undefined |
| array.findIndex(function(item, index) { return 条件 })      | 遍历查找  | 下标 / -1            |

let arr = [1, 5, 7, 3, 10, 2, 4] // 1. forEach可以用于遍历一个数组, 每个元素都会执行一次函数
arr.forEach(function(item, index) { console.log(item, index) }) // 2. map() 映射, 遍历数组, 收集每次函数return的结果 - 返回全新数组
let resultArr = arr.map(function(item, index) { return item * item }) console.log(resultArr) // 3. filter() - 过滤 - 遍历数组, 收集return true的结果, 返回一个新数组
let result2Arr = arr.filter(function(item, index) { return item > 5 }) console.log(result2Arr) // 4. reduce 累加运算 - 遍历数组,需要返回累加的值, 可以进行累加操作 // arr.reduce(function(sum, item, index) { .. }, 起始累加值)
let result3 = arr.reduce(function(sum, item, index) { return sum + item }, 0) console.log(result3) // 5. every 每个 // 作用: 遍历一个数组, 每个元素都会执行一次函数, 必须每次执行都返回true, 最终才会返回true
let flag = arr.every(function(item, index) { return item > 0 }) console.log(flag) // 6. some 某个 // 作用: 遍历一个数组, 每个元素都会执行一次函数, 只要有一次执行返回true, 结果就是true
let flag2 = arr.some(function(item, index) { return item > 8 }) console.log(flag2) // 7. Array.from(伪数组) 作用: 将伪数组转换成真数组
let divs = document.querySelectorAll('div') Array.from(divs).some(function (item, index) { if (item.className === 'active') { return true } else { return false } }) // ----------------------------------------------------------- // 8. find 找第一个符合条件的项, 没找到会返回 undefined
let arr2 = [ { name: 'zs', score: 100 }, { name: 'ls', score: 99 }, { name: 'zs', score: 100 } ] let obj = arr2.find(function(item, index) { return item.score === 100 }) console.log(obj) // 9. findIndex 找第一个符合条件项的下标, 没找到会返回 -1
let index = arr2.findIndex(function(item, index) { return item.score === 101 }) console.log(index)

 

 
免责声明 ES5 新增数组方法 ES6 新增数组方法,资源类别:文本, 浏览次数:43 次, 文件大小:-- , 由本站蜘蛛搜索收录2022-11-24 11:43:01。此页面由程序自动采集,只作交流和学习使用,本站不储存任何资源文件,如有侵权内容请联系我们举报删除, 感谢您对本站的支持。 原文链接:https://www.cnblogs.com/JAG2671169285/p/16923865.html