首页 > 其他分享 >es6 findIndex,find用法

es6 findIndex,find用法

时间:2022-08-24 22:57:15浏览次数:77  
标签:es6 findIndex name index age arr item test3 find


let arr = [
    {name:'test1',age:1},
    {name:'test2',age:2},
    {name:'test3',age:3}
]

let temp = arr.findIndex(function(item){
    console.log(item.name)
    return item.name == 'test3'// 这里return不可少,必须“==”
})
console.log('temp=',temp)


// var arr = [12, 16, 18, 20]
// let index = arr.findIndex(item => item == 16)
// console.log('index=',index)//1


let arr = [
    {name:'test1',age:1},
    {name:'test2',age:2},
    {name:'test3',age:3}
]

// 需求:获取name=test3的元素下标
var index = arr.findIndex(item => {return item.name == 'test3'})
console.log(index)

// 需求:获取name=test3的元素
var obj = arr.find(item => {return item.name == 'test3'})
console.log(obj)

标签:es6,findIndex,name,index,age,arr,item,test3,find
From: https://www.cnblogs.com/pansidong/p/16622558.html

相关文章

  • ES678新特性
    ECMAScript介绍ECMA:欧洲计算机制造商协会(EuropeanComputerManufacturersAssociation)。ECMAScript:由ECMA国标制定的脚本程序设计语言ES6新特性let关键字:变量声明......
  • ERROR: Could not find a version that satisfies the requirement Crypto.Cipher (fr
    换成这个pipinstallpycryptodomePS:之前手动下载Crypto.zip解压到D:\python\Python39\Lib\site-packages\Crypto目录,不知道跟这有没有关系,要是改其他名会报错!OK!!......
  • 前端编译报Error: Cannot find module 'node-sass'
    解决办法:1.在项目目录cmd下运行:npm install -g cnpm --registry=https://registry.npm.taobao.org2.下载成功后再运行:cnpm install node-sass3、两个都下载成......
  • es6 class类中可以直接写赋值语句
    -classCar{//类中考科一直接写赋值语句,如下代码的含义是:给Car的实例对象添加一个属性,名为a,值为1a=1;b}console.log(Car);letc=newCar()console.log......
  • js实现 find 函数
    //arr:要查找的数组,predict:要查找的key字符串或[key,value]数组,或对象{key,value},fromIndex:要从数组中第一个元素开始查,默认为0functionfind(arr,predict,......
  • re.findall()及for..in..迭代器_05
    re.findall(pattern,string,flags=0)返回pattern在string中的所有非重叠匹配,以字符串列表或字符串元组列表的形式。对string的扫描从左至右,匹配结果按照找到......
  • basic_find
    一直对输入数据进行跟踪发现整体的混淆架构main是一个类似于一种switch的vm这个题有一个特征就是如果这题里面一个函数真正有用的话,他会使用很多switch来掩饰而如果......
  • es6 class类内部的方法自动开启了严格模式
    -今天看到react类组件内部的方法中this为undefined,原因是class类内部的方法,自动开启了局部严格模式,不是babel的问题特此实验一下://类内部的方法,自动开启了局部严格模......
  • [Google] LeetCode 2115 Find All Possible Recipes from Given Supplies
    Youhaveinformationaboutndifferentrecipes.Youaregivenastringarrayrecipesanda2Dstringarrayingredients.The\(i\)-threcipehasthenamerecipe......
  • github_findpath_v1.0-Github开源项目目录爆破程序
    Github开源项目目录爆破程序​ 写了个小工具,欢迎师傅们提建议​ 某一天回我的母校溜达了一圈,然后用GoogleHack找到了一个后台,用Wappalyzer没识别到CMS,但是看着这东西......