赞
踩
是否要使用标识符或唯一字段从记录数组中查找记录?
是否要筛选出与特定条件匹配的记录?我们将在本教程中探讨所有这些。
考虑以下员工数组:
const employees = [ { id: 1, first_name: "Jonas", last_name: "Baudasso", email: "jbaudasso0@paypal.com", gender: "Male", department: "Support", }, { id: 2, first_name: "Delcina", last_name: "Baldelli", email: "dbaldelli1@istockphoto.com", gender: "Female", department: "Engineering", }, { id: 3, first_name: "Charlotta", last_name: "Sodor", email: "csodor2@businessinsider.com", gender: "Female", department: "Human Resources", }, { id: 4, first_name: "Scarface", last_name: "Sandercock", email: "ssandercock3@ovh.net", gender: "Male", department: "Support", }, { id: 5, first_name: "Rob", last_name: "Beurich", email: "rbeurich4@comsenz.com", gender: "Male", department: "Engineering", }, ]
我们将在示例中使用上面的列表。
我们可以使用 findIndex
方法来获取第一个匹配记录的索引。
- const index = employees.findIndex(employee => {
- // Function gets called for each employee until the function returns true
- if (employee.id === 3) {
- return true
- }
- return false
- })
-
- console.log(index) // 声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/113429推荐阅读
相关标签
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。