es6中find()怎么用

在es6中,find()用于通过回调函数查找数组中符合条件的第一个元素的值,语法“array.find(function(…),thisValue)”。find()会为数组中的每个元素都调用一次函数执行,当数组中的元素在测试条件时返回true时,find()返回符合条件的该元素,之后的值不会再调用执行函数;如果没有符合条件的元素返回undefined。

es6中find()怎么用

本教程操作环境:windows7系统、ECMAScript 6版、Dell G3电脑。

es6 find()的介绍

find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。

find() 方法为数组中的每个元素都调用一次函数执行:

当数组中的元素在测试条件时返回 true 时, find() 返回符合条件的元素,之后的值不会再调用执行函数。

如果没有符合条件的元素返回 undefined

语法:

array.find(function(currentValue, index, arr),thisValue)

登录后复制

参数 描述

function(currentValue, index,arr)必需。数组每个元素需要执行的函数。
函数参数:参数描述currentValue必需。当前元素index可选。当前元素的索引值arr可选。当前元素所属的数组对象thisValue可选。 传递给函数的值一般用 “this” 值。
如果这个参数为空, “undefined” 会传递给 “this” 值

返回值:返回符合测试条件的第一个数组元素值,如果没有符合条件的则返回 undefined。    

注意: 

find() 对于空数组,函数是不会执行的。

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;};

登录后复制

【相关推荐:javascript视频教程、编程视频】

以上就是es6中find()怎么用的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2924814.html

(0)
上一篇 2025年3月11日 19:00:40
下一篇 2025年2月21日 16:52:37

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • JavaScript更新到了es几

    JavaScript更新到了es13了。2022年6月22日,第123届Ecma大会批准了ECMAScript2022语言规范,这意味着它现在正式成为JavaScript标准;而ECMAScript2022是第13次迭代,因此也可称为ECM…

    2025年3月11日
    200
  • JavaScript普通函数有原型吗

    JavaScript普通函数有原型。在JavaScript中,任何一个函数都有一个prototype(原型)属性,这个属性指向函数的原型对象。原型的作用其实就是为类(函数)提供了一个“公共区域”,在这个公共区域中声明的属性和方法能够被所有通…

    2025年3月11日 编程技术
    200
  • es6 map成员是唯一的么

    es6 map成员是唯一的。ES6新增的Map数据结构类似于对象,key值不限于字符串,成员值唯一;Map结构提供了“值—值”的对应,是一种更完善的Hash结构实现。Map对象保存键值对,并且能够记住键的原始插入顺序;任何值(对象或者原始值…

    2025年3月11日
    200
  • es6怎么实现字符串反转

    实现方法:1、用split、reverse和join函数,语法“str.split(”).reverse().join(”);”;2、用递减的for循环,语法“for(i=字符串长度-1;i>=0;i&#821…

    2025年3月11日
    200
  • es6怎么找出2个数组中不同项

    步骤:1、将两个数组分别转为set类型,语法“newA=new Set(a);newB=new Set(b);”;2、利用has()和filter()求差集,语法“new Set([…newA].filter(x =>!n…

    2025年3月11日 编程技术
    200
  • es6中有没有&符号

    有&符号。在es6中,“&&”是逻辑与运算符,是一种AND布尔操作,语法为“操作数1 && 操作数2”;只有两个操作数都为true时,才返回true,否则返回false。逻辑与是一种短路逻辑,如果左侧…

    2025年3月11日
    200
  • jquery是库吗

    jquery是库。jquery是一个优秀的JavaScript代码库,是为了简化JS的开发或者DOM等操作而开发的一种类库;它封装了JS常用的功能代码(函数),提供一种简便的JS设计模式,优化了HTML文档操作、事件处理、动画设计、Ajax…

    2025年3月11日
    200
  • es6中什么是类的静态成员

    在es6中,由类直接调用的属性和方法叫静态成员。在类里面对变量、函数加static关键字,那它就是静态成员;静态成员不会被实例化成为新对象的元素。静态成员和实例成员的区别:1、实例成员属于具体的对象,而静态成员为所有对象共享;2、静态成员是…

    2025年3月11日
    200
  • es6 map有序吗

    map是有序的。ES6中的map类型是一种储存着许多键值对的有序列表,其中的键名和对应的值支持所有数据类型;键名的等价性判断是通过调用“Objext.is()”方法来实现的,所以数字5与字符串“5”会被判定为两种类型,可以分别作为两种独立的…

    2025年3月11日
    200
  • promise是es6的吗

    是的。promise是ECMAScript 6新增的引用类型,表示一个异步操作的最终完成或者失败。promise是解决异步编程调用代码逻辑编写过于复杂的问题的,当网络请求非常复杂时,就会出现回调地狱,这样如果将这些代码写在一起就会看起来很复…

    2025年3月11日
    200

发表回复

登录后才能评论