Foreach & add remove elements
参考
http://stackoverflow.com/questions/11058384/how-to-delete-specific-array-elements-from-within-a-foreach-loop-in-javascript
https://gist.github.com/chad3814/2924672
for(var f in fruit) {
if ( fruit[f] == "pear" ) {
fruit.splice(f, 1);
}
}
for(var f in fruit) {
document.write(fruit[f]+"<br>");
}
//
apple
pear
banana
you should handle it in this way
var fruit = ["apple", "pear", "pear", "pear", "banana"],
i;
for (i = 0; i < fruit.length; ++i) {
if (fruit[i] === "pear") {
fruit.splice(i--, 1);
}
}
console.log(fruit);
//["apple", "banana"]
原理很显然
当我删除第一个pear的时候 数组是apple pear pear banna
此时继续遍历 将从[2]下标开始 有一个pear就会被忽略
Foreach & add remove elements的更多相关文章
- 有关集合的foreach循环里的add/remove
转自:Hollis(微信号:hollischuang) 在阿里巴巴Java开发手册中,有这样一条规定: 但是手册中并没有给出具体原因,本文就来深入分析一下该规定背后的思考. 1 .foreach循环 ...
- Java ArrayList在foreach中remove的问题分析
目录 iterator itr.hasNext 和 itr.next 实现 倒数第二个元素的特殊 如何避坑 都说ArrayList在用foreach循环的时候,不能add元素,也不能remove元素, ...
- WIX: Hide installed program from the Add/Remove Programs window.
Reference article : How to hide an entry in the Add/Remove Programs applet? In Wix source files, set ...
- How to hide an entry in the Add/Remove Programs applet?
Original link: http://www.winhelponline.com/articles/15/1/How-to-hide-an-entry-in-the-AddRemove-Prog ...
- Maste Note for OCR / Vote disk Maintenance Operations (ADD/REMOVE/REPLACE/MOVE)
Doc ID 428681.1 Applies to: Oracle Database - Enterprise Edition - Version 10.2.0.1 to 11.2.0.1.0 [R ...
- Javacript Remove Elements from Array
參考自: https://love2dev.com/blog/javascript-remove-from-array/ 1. Removing Elements from End of Array ...
- HashSet——add remove contains方法底层代码分析(hashCode equals 方法的重写)
引言:我们都知道HashSet这个类有add remove contains方法,但是我们要深刻理解到底是怎么判断它是否重复加入了,什么时候才移除,什么时候才算是包括????????? add ...
- 阿里规范学习总结-不要再foreach对元素进行add()/remove()操作,
在foreach循环中,对元素进行 remove()/add() 操作需要使用Iterator ,如果运行在多线程环境下,需要对Iterator对象枷锁. public class ForeachTe ...
- SharePoint自动化系列——Add/Remove "Record" from items
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 目的:批量的将SharePoint items变成records或者将records变成普通的it ...
随机推荐
- Git操作流水账
一.关于Git Git是一个分布式版本控制/软件配置管理软件,原是Linux内核开发者林纳斯·托瓦兹(Linus Torvalds)为更好地管理Linux内核开发而设计. 二.Git的环境配置 2.1 ...
- [key]严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener(Spring配置异常)
详细错误为: 严重: Exception sending context initialized event to listener instance of class org.springframe ...
- Thinkphp利用微信多客服消息推送取货二维码消息
首先看微信官方的说法: 当用户主动发消息给公众号的时候(包括发送信息.点击自定义菜单.订阅事件.扫描二维码事件.支付成功事件.用户维权), 微信将会把消息数据推送给开发者,开发者在一段时间内(目前修改 ...
- Linux学习之nfs安装配置
安装疑难问题: 1.检查系统是否开启nfs服务:service nfs status 结果显示nfs: unrecognized service,说明系统没有安装nfs服务,so 安装之: 注意,yu ...
- Scala类型参数中协变(+)、逆变(-)、类型上界(<:)和类型下界(>:)的使用
转自:http://fineqtbull.iteye.com/blog/477994#bc2364938 有位je上的同学来短信向我问起了Scala类型参数中协变.逆变.类型上界和类型下界的使用方法和 ...
- 深入Android媒体存储服务(二):磁盘扫描流程
简介: 本文是<深入Android媒体存储服务>系列第二篇,简要介绍媒体存储服务扫描文件的流程.文中介绍的是 Android 4.2. Android 有一套媒体存储服务,进程名是 and ...
- java 操作配置文件 .properties
package com.dms.common; import java.io.File; import java.io.FileInputStream; import java.io.FileNotF ...
- android 传感器使用 Compass指南针的实现功能
以下是指南针通过方向传感器而旋转实现. CompassDemo.java: package com.example.activity; import android.app.Activity; imp ...
- RAID详细介绍
RAID详细介绍 RAID 0 又称为Stripe或Striping,它代表了所有RAID级别中最高的存储性能.RAID 0提高存储性能的原理是把连续的数据分散到多个磁盘上存取,这样,系统有数据请求就 ...
- git Bug分支
Bug分支 软件开发中,bug就像家常便饭一样.有了bug就需要修复,在Git中,由于分支是如此的强大,所以,每个bug都可以通过一个新的临时分支来修复,修复后,合并分支,然后将临时分支删除. 当你接 ...