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

 

posted @ 2020-07-29 12:32  伴月阁  阅读(6754)  评论(0编辑  收藏  举报