Drop it
来源:互联网 发布:超级基因优化液毛球 编辑:程序博客网 时间:2024/06/05 14:59
让我们来丢弃数组(arr)的元素,从左边开始,直到回调函数return true就停止。
第二个参数,func,是一个函数。用来测试数组的第一个元素,如果返回fasle,就从数组中抛出该元素(注意:此时数组已被改变),继续测试数组的第一个元素,如果返回fasle,继续抛出,直到返回true。
最后返回数组的剩余部分,如果没有剩余,就返回一个空数组。
思路
用 while() 循环,当 func(arr[0]) 不被满足时移除arr首项。然后继续遍历数组剩余部分。
要注意的是,当 func(arr[0]) 一直不被满足时,arr首项将被无限多(浏览器最大值边界)次移除,即使arr为空数组也会循环此操作。为了保证效率,应加上限制条件arr.length > 0 阻止无限循环的发生。
function drop(arr, func) { while(!func(arr[0])&&arr.length>0){ arr.shift(); } return arr;}drop([1, 2, 3], function(n) {return n < 3; });
阅读全文
0 0
- Drop it
- Drop it
- JS:Drop It!抛弃数组
- drop
- Lesson Thirty-Nine Don't drop it. 别掉了.
- Lesson 39 Don't drop it! 别摔了!
- Cannot drop database "databasename" because it is currently in use.
- PostgreSQL中 drop table指令出现ERROR: cannot drop table userinfo because other objects depend on it
- Error 3724:Cannot drop the table 'object_name' because it is being used for replication.
- Error 3724:Cannot drop the table 'object_name' because it is being used for replication.
- Cannot drop the database 'databasename' because it is currently in use
- Cannot drop the database ‘XXX’ because it is being used for replication.
- Lesson 39 Don't drop it! 别摔了…
- Cannot drop the database [NAME] because it is currently in use
- Could not drop object 'student' because it is referenced by a FOREIGN KEY constraint
- Cannot drop the database XXX because it is being used for replication
- 删除数据库时提示数据库正在被使用,无法删除(Cannot drop database databasename because it is currently in use)。
- 053-22 How can you reverse the effects of an ALTER DISKGROUP … DROP DISK command if it has already
- 讲座倒计时:抓紧报名!还剩1天,20个赠教材名额!
- Palette的使用
- 阿里云CentOS7 搭建apache+php+mysql环境,并配置多站点多域名
- MyBatis(一) 为什么使用框架。
- Redis 主从复制、读写分离、高可用(七)-part 1
- Drop it
- React Native 组件之Touchable*源码解析
- mongodb无法注册windows服务
- mybatis入门基础(五)----动态SQL
- SSL、TLS协议格式、HTTPS通信过程、RDP SSL通信过程
- 正则表达式验证用户名、密码、手机号码、身份证
- Java调用C++编写的动态库(JNI)
- 自定义欢迎界面跳过按钮
- Permutations(主要谈谈如何做全排列,组合等回溯的使用)