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. jdbc_odbc SQLserver 驱动安装及测试

    有2次被问到同一个问题,尽管博客园是.net的园子,我还是分享下吧.PS:我现在做的.net,以前学过点java.献丑了. ------------------ 原始邮件 -------------- ...

  2. 使用Spring MVC,Mybatis框架等创建Java Web项目时各种前期准备的配置文件内容

    1.pom.xml 首先,pom.xml文件,里面包含各种maven的依赖,代码如下: <project xmlns="http://maven.apache.org/POM/4.0. ...

  3. mktime性能问题

    #include <time.h> int main() { for (int i = 0; i < 100000; ++i) { struct tm tm = {}; tm.tm_ ...

  4. jquery元素查找方法

    $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到的是唯一的元素 $("div&q ...

  5. Extjs之rowEditing编辑状态时列不对齐

    Extjs在使用rowEditing的时候,会在每一列加上editor属性,表示当处于编辑状态时这一列的值是什么类型的,后突然发现在rowEditing处于编辑状态时每一列的宽度边框了,如果列数非常多 ...

  6. Bootstrap_排版_标题

    Bootstrap和普通的HTML页面一样,定义标题都是使用标签<h1>到<h6>,只不过Bootstrap覆盖了其默认的样式,使用其在所有浏览器下显示的效果一样,具体定义的规 ...

  7. exit() die return 的区别

    rutrun 会返回一个值,或不返回值 只是当前函数内return后面的语句将不会执行,后面的其它语句继续执行exit()可以返回值,也可以不返回值直接终止程序 函数输出一条消息,并退出当前脚本. 会 ...

  8. 【转】火火火火火!看HomeKit如何改变物联网和智能家居?

    摘要: 智能家居并非新概念,然而在苹果等巨头插足之前,它却只是一盘散沙,各自为营,苹果又将如何凭借HomeKit构建起拥有统一界面和控制中心的平台来实现各种智能家居设备与应用之间的无缝连接,真正实现智 ...

  9. POJ 3356(最短编辑距离问题)

    POJ - 3356 AGTC Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Desc ...

  10. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...