[英]Check if 2d Array Index Exists
I have a 8x8 2 dimensional array named positions
and I'm trying to see if there's a way to check if an index is defined or not. 我有一个名为
positions
的8x8二维数组,我试图看看是否有办法检查是否定义了索引。 I saw an example using 我看到了一个使用的例子
if (typeof positions[8] === "undefined") alert('undefined');
but when I try it using an index for a row and column like this: 但是当我尝试使用像这样的行和列的索引时:
if (typeof positions[8][0] === "undefined") alert('undefined');
I'm getting error: 我收到错误:
Uncaught TypeError: Cannot read property '0' of undefined
Is there a way to check if an index for a 2D array exists, not only for one index but for both its dimensions? 有没有办法检查2D数组的索引是否存在,不仅对于一个索引而且对于它的两个维度?
The problem is that the row doesn't exist you cannot access elements in it. 问题是该行不存在您无法访问其中的元素。
A solution is 一个解决方案是
if ((positions[8]||[])[0] === undefined) { ... }
the idea is that undefined
is falsy, thus position[8]||[]
will return an empty list if row 8 is not present and the second indexing will not raise an error. 这个想法是
undefined
是假的,因此如果第8行不存在而且第二个索引不会引发错误, position[8]||[]
将返回一个空列表。
The formal problem of that expression is that in case position[8]
is indeed undefined
a new temporary empty row is allocated just to try to access it. 该表达式的形式问题是,如果
position[8]
确实undefined
则仅分配新的临时空行以尝试访问它。 This could be in theory solved by a smart-enough compiler (and may be it is in some implementation) but another option without this temporary allocation could instead be 这可以在理论上通过足够智能的编译器来解决(并且可能是在某些实现中)但是没有这种临时分配的另一种选择可能是
if (!position[8] || position[8][0] === undefined) { ... }
where he problem is instead that both position
and [8]
inside the result are looked up twice (if position
is a global this can require a dictionary lookup and, even worse than that, accessing position[8]
can potentially change what position
will resolve to in next lookup if position
is for example a proxy). 问题在于,结果中的
position
和[8]
都被查找两次(如果position
是全局的,这可能需要字典查找,更糟糕的是,访问position[8]
可能会改变将解决的position
如果position
是例如代理,则在下一次查找中。 An even more explicit 更加明确
{
let row = position[8];
if (row === undefined || row[0] === undefined) { ... }
}
should AFAIK solve these problems but is however even more verbose and hard to read and probably a bad idea in most cases. AFAIK应该解决这些问题但是更加冗长和难以阅读,在大多数情况下可能是个坏主意。
PS: given the general orientation of Javascript of just returning undefined
and keeping running it's indeed surprising that undefined[...]
wasn't defined to be undefined
itself instead of raising an error. PS:鉴于Javascript的一般方向只是返回
undefined
并保持运行,确实令人惊讶的是undefined[...]
undefined
本身undefined
而不是引发错误。
PPS: this problem of accessing an element in a potentially non-existent array, field in a potentially non-existent object or calling a potentially non-existing function is becoming popular and may be in a few months there will be (or there already is) a proposal about adding something like position[?8]===undefined
to mean exactly this kind of somewhat common computation. PPS:访问潜在不存在的数组中的元素,可能不存在的对象中的字段或调用可能不存在的函数的这个问题正变得越来越流行,可能会在几个月后出现(或者已经存在) )关于添加类似
position[?8]===undefined
的提议,正好意味着这种有点常见的计算。 But as far as I know this didn't make it into Javascript (yet). 但据我所知,这并没有成为Javascript(尚未)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.