为什么typeof的数组的数组的未定义值返回为“0”;未定义的“;我的条件不认为是真的

Why is an undefined value of an array of array that typeof returns as "undefined" not considered true by my conditional?

本文关键字:未定义 数组 我的 条件 不认为 真的 认为是 typeof 返回 为什么      更新时间:2023-09-26

为什么typeof返回为"undefined"的数组的未定义值不被我的条件视为true?这与OR运算符有关吗。我的程序似乎正在运行内部条件,尽管它不应该在内部条件中。

if(typeof elemData !== 'undefined' || typeof rich[elemData.value] !== 'undefined') {
    if(typeof rich[elemData.value]['title'] !== 'undefined') {
        //do something
    }
}

程序返回以下内容:

> if(typeof rich[elemData.value]['title'] !== 'undefined') {
> 
> TypeError: Cannot read property 'title' of undefined

我正在检查rich[elemData.value]是否是'undefined',它说它不是通过我的条件。怎么回事?

当需要对条件进行AND运算时,您正在对条件进行OR运算。

if(typeof elemData !== 'undefined' || typeof rich[elemData.value] !== 'undefined') {

应为:

if(typeof elemData !== 'undefined' && typeof rich[elemData.value] !== 'undefined') {