我有一个像这样的数组
a=[1,2,3,,4,5];
所以现在我想找到缺失值索引,即3
, using indexOf
.
检查该值是否为undefined
就像下面的代码:
for ( var i = 0; i < a.length; i++ ) {
if ( typeof a[i] === "undefined" ) {
// do stuff here or break the loop
}
}
Update你也可以这样做:
Array.prototype.indexOfUndefined = function() {
for ( var i = 0; i < this.length; i++ ) {
if ( typeof this[i] === "undefined" ) {
return i;
}
}
}
你需要返回i
因为i
是当前索引,它将搜索第一个undefined
value.
Demo: http://jsfiddle.net/vdyypq6o/5/ http://jsfiddle.net/vdyypq6o/5/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)