find

基本使用

Array.prototype.find
返回第一个满足条件的数组元素

const arr = [1, 2, 3, 4, 5];
const item = arr.find(function (item) {return item > 3;
});console.log(item);//4

如果没有一个元素满足条件 返回undefined

const arr = [1, 2, 3, 4, 5];
const item = arr.find(function (item) {return item > 5;
});console.log(item); //undefined

返回的元素和数组对应下标的元素是同一个引用

const arr = [{id: 1,name: '张三',},{id: 2,name: '李四',},{id: 3,name: '王五',},
];const item = arr.find((item) => item.name === '李四');
console.log(item);


回调函数的返回值是boolean 第一个返回true的对应数组元素作为find的返回值

const arr = [{id: 1,name: '张三',},{id: 2,name: '李四',},{id: 3,name: '王五',},
];
const item = arr.find(function (item) {return item.id > 1;
});
console.log(item);

回调的参数

当前遍历的元素 当前遍历出的元素对应的下标 当前的数组

const arr = [{id: 1,name: '张三',},{id: 2,name: '李四',},{id: 3,name: '王五',},
];
const item = arr.find(function (item, index, arr) {console.log(item, index, arr);
});

find的第二个参数

更改回调函数内部的this指向

const arr = [{id: 1,name: '张三',},{id: 2,name: '李四',},{id: 3,name: '王五',},
];
const item = arr.find(function (item, index, arr) {console.log(item, index, arr);console.log(this);},{ a: 1 }
);


如果没有第二个参数
非严格模式下 this -> window

const arr = [{id: 1,name: '张三',},{id: 2,name: '李四',},{id: 3,name: '王五',},
];
const item = arr.find(function (item, index, arr) {console.log(item, index, arr);console.log(this);
});


在严格模式下
不传入第二个参数 this为undefined 与严格模式规定相同

'use strict';
const arr = [{id: 1,name: '张三',},{id: 2,name: '李四',},{id: 3,name: '王五',},
];
const item = arr.find(function (item, index, arr) {console.log(item, index, arr);console.log(this);
});

稀疏数组find

find会遍历稀疏数组的空隙 empty
具体遍历出的值 由undefined占位

const arr = Array(5);
arr[0] = 1;
arr[2] = 3;
arr[4] = 5;
const item = arr.find(function (item) {console.log(item);return false;
});


而ES5数组扩展方法forEach,map,filter,reduce,reduceRight,every,some 只会遍历有值的数组
find的遍历效率是低于ES5数组扩展方法的

find不会更改数组

虽然新增了元素 但是find会在第一次执行回调函数的时候 拿到这个数组最初的索引范围

const arr = [1, 2, 3, 4, 5];
const item = arr.find(function (item) {arr.push(6);console.log(item);
});
console.log(arr);

const arr = [1, 2, 3, 4, 5];
const item = arr.find(function (item) {arr.splice(1, 1);console.log(item);
});
console.log(arr);


splice 删除对应项 该项位置不保留 在数据最后补上undefined

const arr = [1, 2, 3, , , , 7, 8, 9];
arr.find(function (item, index) {if (index === 0) {arr.splice(1, 1);}console.log(item);
});


delete
删除该项的值 并填入undefined

const arr = [1, 2, 3, , , , 7, 8, 9];
arr.find(function (item, index) {if (index === 0) {delete arr[2];}console.log(item);
});


pop
删除该项的值 并填入undefined

const arr = [1, 2, 3, , , , 7, 8, 9];
arr.find(function (item, index) {if (index === 0) {arr.pop();}console.log(item);
});

创建myFind

Array.prototype.myFind = function (cb) {if (this === null) {throw new TypeError('"this" is null');}if (typeof cb !== 'function') {throw new TypeError('Callback must be a function type');}var obj = Object(this),len = obj.length >>> 0,arg2 = arguments[1],step = 0;while (step < len) {var value = obj[step];if (cb.apply(arg2, [value, step, obj])) {return value;}step++;}return undefined;
};

ES6 -- find 详解相关推荐

  1. ES6 Promise详解

    Promise是JS中进行异步编程的新解决方案. 从语法上来说:Promise是一个构造函数 从功能上来说:promise对象用来封装一个异步操作并可以获取其成功/失败的结果值 promise:启动异 ...

  2. ES6 -- fill详解

    fill Array.prototype.fill fill方法实际上是根据下标范围来给范围内的元素覆盖新的值 左闭右开 const arr = [1, 2, 3, 4, 5]; const newA ...

  3. ES5和ES6数组遍历方法详解

    ES5和ES6数组遍历方法详解 在ES5中常用的10种数组遍历方法: 1.原始的for循环语句 2.Array.prototype.forEach数组对象内置方法 3.Array.prototype. ...

  4. 【ES6】Module模块详解

    [ES6]Module模块详解 一.Module的由来 二.严格模式 三.export命令 四.import命令 查看更多ES6教学文章: 参考文献 引言:由于两个JS文件之间相互使用必须通过一个ht ...

  5. 【ES6】Promise对象详解

    [ES6]Promise对象详解 一.Promise对象的含义 二.Promise对象的用法 三.Promise对象的几个应用[重点] 1.时间延迟函数 2.图片异步加载 查看更多ES6教学文章: 参 ...

  6. 【ES6】Generator函数详解

    [ES6]Generator函数详解 一.Generator函数简介 基本概念 函数写法 yield关键字介绍 二.next方法的参数 三.for...of循环 四.关于普通throw()与Gener ...

  7. 【ES6】 let与const详解

    [ES6] let与const详解 ES6,全称为ECMAScript6.ES6与js的关系是:前者是后者的规格,后者是前者的实现.换句话说,ES是js的国际化版本,js是ES的子集. 1. let ...

  8. JS ES6中export和import详解

    1.Export 模块是独立的文件,该文件内部的所有的变量外部都无法获取.如果希望获取某个变量,必须通过export输出, // profile.js export var firstName = ' ...

  9. ES6模块之export和import详解

    ES6模块之export和import详解 ES6中的模块即使一个包含JS代码的文件,在这个模块中所有的变量都是对其他模块不可见的,除非我们导出它.ES6的模块系统大致分为导出(export)和导入( ...

最新文章

  1. GPT3 api接口调用
  2. 如何安装和使用RAutomation
  3. [转]C++结构体|类 内存对齐详解
  4. 如何修改SharePoint服务器场管理员帐户和密码
  5. adb remount overlayfs的说明
  6. 《java系统性能调优》--1.发现瓶颈
  7. Python自省(反射)指南 1
  8. dagger2 注入_Android依赖注入– Dagger 2
  9. php mysql 读取数据_PHP MySQL 读取数据
  10. 【转】 VS2005中ajax安装指南
  11. IE8兼容html5视频播放
  12. 网络分析优化顶点覆盖Vertex Cover算法初探
  13. PHP TP模板下的微博登录(wap)
  14. Leetcode PHP题解D1:宝石与石头
  15. 页面获取服务器图片路径问题
  16. 【牛客网华为机试】HJ69 矩阵乘法
  17. 划水总结剑指offer 链表系列1
  18. 最全的IMSI获取手机号段、归属地java代码
  19. 计算机基础操作测试题,计算机基础操作练习题.pdf
  20. HTML5新特性之History

热门文章

  1. 第六章 组合数据类型----元组
  2. matlab是什么意思,x'在matlab是什么意思
  3. MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细
  4. steamvr自定义按键_Steam入门手册:教你如何自定义Steam VR中的手柄皮肤
  5. Java中怎么获取文件夹的名称_java获取文件夹下所有文件的名称
  6. MVC模式计算两个正数的代数平均值与几何平均值
  7. Failed to resolve directive: XXX
  8. 计算机应用基础自主学习,计算机应用基础络自主学习平台使用说明.doc
  9. 沃利斯圆周率用c语言,沃利斯圆周率计算公式!
  10. ReactiveX 简介