检查在JavaScript数组中的数字索引 [英] Checking for a numerical index in a javascript array

查看:145
本文介绍了检查在JavaScript数组中的数字索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林接收由数字指标汇总JSON数据。因为0犯规存在时林在我forloop,例如,索引可能为1,这意味着在我forloop启动会发生错误。我如何检查,如果JavaScript数组中存在一个数字索引?


解决方案

  VAR一个= [1,2,3]
    索引= 2;如果(A [指数]!==未定义){
    //指数不指向一个未定义的项目。
}

Im receiving json data that is aggregated by numerical indexes. When Im in my forloop, for example, the index might start at 1, which means in my forloop an error would occur because 0 doesnt exist. How do I check if a numerical index exists in the javascript array?

解决方案

var a = [1, 2, 3],
    index = 2;

if ( a[index] !== undefined ) {
    // index doesn't point to an undefined item.
}

这篇关于检查在JavaScript数组中的数字索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆