Zen*_*noo 3 javascript arrays indexing null
我有一个内置已定义和null值的数组,如下所示:
var arr = [
{...},
null,
null,
{...},
null
];
Run Code Online (Sandbox Code Playgroud)
有什么方法可以从这个数组中获取最后一个非null元素的索引吗?我的意思是不必完全循环它.
你可以使用while
循环并从最后迭代.
var array = [{ foo: 0 }, null, null, { bar: 42 }, null],
index = array.length;
while (index-- && !array[index]);
console.log(index);
console.log(array[index]);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2612 次 |
最近记录: |