利用ES6中的Array.find/ Array.findIndex来判断数组中已存在某个对象
前端开发过程中,我们会经常遇到这样的情景:比如选中某个指标obj,将其加入到数组checkedArr中({id: 1234, name: 'zzz', ...}),但是在将其选中之前要校验该指标是否已经被选择。
以前的思路是:循环数组checkedArr,如果checkedArr[i].id===obj.id,则说明该指标已经在数组中了。
在ES6中,数组得到扩展,新增了find和findIndex两个方法,可以用到这个情境中:
-
find()方法返回数组中满足提供的测试函数的第一个元素的值。否则返回undefined。 -
findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1。
find方法:
var objArr = [{id:1, name:'jiankian'}, {id:23, name:'anan'}, {id:188, name:'superme'}, {id:233, name:'jobs'}, {id:288, name:'bill', age:89}, {id:333}] ;
var ret2 = objArr.find((v) => {
return v.id == 233;
});
console.log(ret2);
// return {id:233, name:'jobs'}
// 当返回undefined时,则说明objArr中没有,可以添加
findIndex方法:
var objArr = [{id:1, name:'jiankian'}, {id:23, name:'anan'}, {id:188, name:'superme'}, {id:233, name:'jobs'}, {id:288, name:'bill', age:89}, {id:333}] ;
var ret2 = objArr.findIndex((v) => {
return v.id == 233;
});
console.log(ret2);
// return 3
// 当返回-1时,则说明objArr中没有,可以添加了
利用ES6中的Array.find/ Array.findIndex来判断数组中已存在某个对象的更多相关文章
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 找到旋转后有序数组中的最小值
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4032570.html 原题: Suppose a sorted array is ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- LeetCode 80 Remove Duplicates from Sorted Array II(移除数组中出现两次以上的元素)
题目链接:https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/#/description 给定一个已经排好序的数组 ...
- 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)
题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/#/description 姊妹篇:http://www. ...
- LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)
题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description 从有序数组中移除重 ...
- PHP 中替换若干字符串字串为数组中的值,不用循环,非常高效
替换某个字符串中的一个或若干个字串为数组中某些值 php本身有自带的函数,可以不用循环非常高效的实现其效果: 实例代码: $phrase = "You should eat fruit ...
随机推荐
- Minix2.0内核源代码的组织结构
Minix2.0操作系统的源代码由两个目录组成:include/目录和src/目录 include/目录包含了操作系统所有的头文件(即.h文件) src/目录下包含了操作系统所有的源文件(.c或.s文 ...
- Eclipse打包Egret App (Egret4.1.0)
Egret官方提供eclipse和androidstudio打包. 这里使用eclipse. 1 下载配置android环境 2 Egret打包App 3 Eclipse设置 4 Eclipse调试 ...
- log file sync 事件(转)
log file sync log file sync等待时间发生在redo log从log buffer写入到log file期间. 下面对log file sync做个详细的解释. 何时发 ...
- 求树的直径和中心(ZOJ3820)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5374 Building Fire Stations Time Limit: 5 ...
- JavaMVC框架之SpringMVC
欢迎查看Java开发之上帝之眼系列教程,如果您正在为Java后端庞大的体系所困扰,如果您正在为各种繁出不穷的技术和各种框架所迷茫,那么本系列文章将带您窥探Java庞大的体系.本系列教程希望您能站在上帝 ...
- 3.html+.ashx(删除学生信息)
C03ListStu.ashx 0:false(删除);1:true(正常). (数据库里定义个BOOL型,TRUE表示正常FALSE表示删除) <html> <head> & ...
- Gitlab备份和恢复操作
参考:https://www.cnblogs.com/kevingrace/p/7821529.html 一,设置开启备份以及备份路径 /etc/gitlab/gitlab.rb gitlab_rai ...
- JavaWeb404排错的小技巧
报这种错误,404后面什么都没有的话,就证明处理器映射器根据url找不到handler. 报这种错误,证明处理器映射器根据url找到了handler,转发的jsp页面找不到,说明jsp页面错了.
- git-【十】忽略文件
1.在Git工作区的根目录下创建一个特殊的.gitignore文件,然后把要忽略的文件名填进去,Git就会自动忽略这些文件. 不需要从头写.gitignore文件,GitHub已经为我们准备了各种配置 ...
- 一个Spark job的生命历程
一个job的生命历程 dagScheduler.runJob //(1) --> submitJob ( eventProcessLoop.post(JobSubmitted,***) //(2 ...