如何找到在阵列的缺失值的索引? [英] How to find the index of a missing value in an array?
本文介绍了如何找到在阵列的缺失值的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像
a=[1,2,3,,4,5];
所以现在我要找到失踪的价值指标,即 3
,使用的indexOf
。
推荐答案
检查值未定义
像下面的code:
Check if the value is undefined
like the following code:
for ( var i = 0; i < a.length; i++ ) {
if ( typeof a[i] === "undefined" ) {
// do stuff here or break the loop
}
}
更新
你也可以这样做:
Update You can do this too:
Array.prototype.indexOfUndefined = function() {
for ( var i = 0; i < this.length; i++ ) {
if ( typeof this[i] === "undefined" ) {
return i;
}
}
}
您需要返回 I
,因为 I
是当前索引,它会搜索第一个未定义
值。
You need to return i
because i
is the current index, it will search for the first undefined
value.
演示: http://jsfiddle.net/vdyypq6o/5/
这篇关于如何找到在阵列的缺失值的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文