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 ...
随机推荐
- C#根据汉字生成拼音首字母全称
static void Main(string[] args) { string s = GetChineseSpell("周杰伦"); Console.WriteLine(s.T ...
- poj1323--贪心算法
题意:一群人打牌包括你,每人出一张牌,谁最大,谁就算赢一局,问你最少能赢几局? 给出人数N,每人的牌数M,及你的牌. 分析:1.这题需比较大小,就像我们打牌时要将牌排序以便出牌,显然要先将手上的牌进行 ...
- C++ 数据结构学习二(单链表)
模板类 //LinkList.h 单链表#ifndef LINK_LIST_HXX#define LINK_LIST_HXX#include <iostream>using namespa ...
- sql防注入代码
function defend_sql($string, $force = 1) { $preg = "select|insert|and|or|update|delete|\'|\/\*| ...
- JVM学习之JVM1.6 GC详解
转自:http://www.cnblogs.com/ggjucheng/p/3977384.html,多谢分享 前言 JVM GC是JVM的内存回收算法,调整JVM GC(Garbage Colle ...
- Linux学习之常用技巧
▌基础 学习 Bash .你可以man bash来看看bash的东西,并不复杂也并不长.你用别的shell也行,但是bash是很强大的并且也是系统默认的.(学习zsh或tsch只会让你在很多情况下受到 ...
- HTTP填坑
HTTP知识填坑 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB",& ...
- artTemplate-3.0
https://github.com/aui/artTemplate artTemplate-3.0 新一代 javascript 模板引擎 目录 特性 快速上手 模板语法 下载 方法 NodeJS ...
- 网站linux.linuxidc.com有很多好资料
免费下载地址在 http://linux.linuxidc.com/ 用户名与密码都是www.linuxidc.com 有一些介绍:www.linuxidc.com/download
- TelephonyManager
每台手机有一个唯一的设备ID