JSON 对象数组快速查出数组中的某个对象


使用JS数组的"find()"和"findIndex()"方法

  find() 方法返回数组中满足提供的测试函数的第一个元素的值。没有则返回 undefined

  findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。没有则返回-1。

1     let json=[{name:"张三",pass:"123456"},{name:"李四",pass:"222222"},{name:"王五",pass:"333333"},{name:"赵六",pass:"444444"}];
2     json.findIndex(item=>item.name=="赵六");       //3
3     json.findIndex(item=>item.name=="赵六111");     //-1
4     json.find(item=>item.name=="李四111")         //undefined
5     json.find(item=>item.name=="李四")           //{name:"李四",pass:"222222"}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM