The most common way to loop through the elements of an array is with a for loop:

var o = [1,2,3,4,5]
var keys = Object.keys(o);             // Get an array of property names for object o
var values = []                   // Store matching property values in this array
for(var i = 0; i < keys.length; i++) {        // For each index in the array
  var key = keys[i];               // Get the key at that index
  values[i] = o[key];              // Store the value in the values array
}

In nested loops, or other contexts where performance is critical, you may sometimes
see this basic array iteration loop optimized so that the array length is only looked up
once rather than on each iteration:

for(var i = 0, len = keys.length; i < len; i++) {
// loop body remains the same
}

These examples assume that the array is dense and that all elements contain valid data.
If this is not the case, you should test the array elements before using them. If you want
to exclude null, undefined, and nonexistent elements, you can write this:

  for(var i = 0; i < a.length; i++) {
    if (!a[i]) continue; // Skip null, undefined, and nonexistent elements
      // loop body here
  }

If you only want to skip undefined and nonexistent elements, you might write:
  for(var i = 0; i < a.length; i++) {
    if (a[i] === undefined) continue; // Skip undefined + nonexistent elements
    // loop body here
  }

Finally, if you only want to skip indexes for which no array element exists but still want
to handle existing undefined elements, do this:
  for(var i = 0; i < a.length; i++) {
    if (!(i in a)) continue ; // Skip nonexistent elements
      // loop body here
  }

You can also use a for/in loop (§5.5.4) with sparse arrays. This loop assigns enumerable property names (including array indexes) to the loop variable one at a time.

Indexes that do not exist will not be iterated:

  for(var index in sparseArray) {
    var value = sparseArray[index];
    // Now do something with index and value
  }

javascript 数组 排除null, undefined, 和不存在的元素的更多相关文章

  1. javascript 数组实例

    在遍历数组时, 如果想要排除 null / undefined 和 不存在的元素时,代码如下: for ( var i = 0; i < a.length; i++ ){ //跳过null / ...

  2. JavaScript数据类型 typeof, null, 和 undefined

    JavaScript 数据类型 在 JavaScript 中有 5 种不同的数据类型: string number boolean object function 3 种对象类型: Object Da ...

  3. JS去除对象或数组中的空值('',null,undefined,[],{})

    javascript去掉对象或数组中的'',null,undefined,[],{}.思路就是创建一个新的空对象,然后对传入的对象进行遍历,只把符合条件的属性返回,保留有效值,然后就相当于把空值去掉了 ...

  4. 【阿里李战】解剖JavaScript中的 null 和 undefined

    在JavaScript开发中,被人问到:null与undefined到底有啥区别? 一时间不好回答,特别是undefined,因为这涉及到undefined的实现原理.于是,细想之后,写下本文,请各位 ...

  5. 区别Javascript中的Null与Undefined

    在JavaScript中存在这样两种原始类型:Null与Undefined.这两种类型常常会使JavaScript的开发人员产生疑惑,在什么时候是Null,什么时候又是Undefined? Undef ...

  6. 【转】Javascript 中的false,零值,null,undefined和空字符串对象

    js 开发中经常会碰到判断是否为空的情况,关于 null 和 undefined 的区别了解的不是很好,刚好看见这篇文章,转过来学习一下,以下是转载正文: 在Javascript中,我们经常会接触到题 ...

  7. Javascript 中的false,零值,null,undefined和空字符串对象

    在Javascript中,我们经常会接触到题目中提到的这5个比较特别的对象--false.0.空字符串.null和undefined.这几个对象很容易用错,因此在使用时必须得小心. 类型检测 我们下来 ...

  8. 解剖JavaScript中的null和undefined【转】

    在JavaScript开发中,被人问到:null与undefined到底有啥区别? 一时间不好回答,特别是undefined,因为这涉及到undefined的实现原理.于是,细想之后,写下本文,请各位 ...

  9. 浅谈JavaScript中的null和undefined

    浅谈JavaScript中的null和undefined null null是JavaScript中的关键字,表示一个特殊值,常用来描述"空值". 对null进行typeof类型运 ...

随机推荐

  1. html.day01

    1.web标准: 1. 结构  (xhtml)  2. 表现(css)  3.行为(js) html   超文本标记语言 xhtml  (严格型超文本标记语言) 2.规范: 1. 所有标签(标记)都要 ...

  2. tinkphp URL重写,支持伪静态

    通常的URL里面含有index.php,为了达到更好的SEO效果可能需要去掉URL里面的index.php ,通过URL重写的方式可以达到这种效果,通常需要服务器开启URL_REWRITE模块才能支持 ...

  3. C语言零移位操作

    给定一个整形数组要求把其中的零元素移动到数组的末尾 非零元顺序保持不变 以下采用两种方法实现 #include <stdlib.h> #include <stdio.h> #i ...

  4. asp.net使用Mysql乱码处理

    在asp.net与mysql数据库打交道的时候,由于配置的问题,会遇到自己写的方法在读取数据库中数据的时候,英文,数字可以正常通过,但是中文就无法通过,以登录为例(方法略),当输入英文用户名的时候可以 ...

  5. sql编程 1

    declare emp_count number;begin select count(*) into emp_count from emp where HIOREDATE >= TO_DATE ...

  6. Linux 下 安装 Phalcon

    先安装GIT 然后从 git://github.com/phalcon/cphalcon.git 这里下载安装文件 编译完成就可以安装了! 编译chmod -R 777 cphalcon1. 创建从C ...

  7. 实验一:C语言实现DES加解密算法

    计算程序执行10万次需要的时间: 总共需要175秒 加解密一次的时间小于:0.00175秒 纯计算加解密的时间会更短 去除IO操作后的时间 也就是说加解密一次的时间为0.07毫秒 /*-------- ...

  8. Android 开发中使用 SQLite 数据库

    SQLite 介绍 SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能. 此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PHP ...

  9. matlab图像类型转换以及uint8、double、im2double、im2uint8和mat2gray等说明

    转自:http://blog.csdn.net/fx677588/article/details/53301740 1. matlab图像保存说明 matlab中读取图片后保存的数据是uint8类型( ...

  10. 简单又强大的联发科手机PhilZ Touch Recovery安装器,详细教程 - 本文出自高州吧

    原文地址:http://bbs.gaozhouba.com/thread-19355-1-1.html * * * * * * * * * * * * * * * * * * * * * * * * ...