Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将对象检查为未定义?_Javascript - Fatal编程技术网

Javascript 如何将对象检查为未定义?

Javascript 如何将对象检查为未定义?,javascript,Javascript,我有一个目标 dataSourceConfig.transport.read.url remoteDataSourceConfig: { transport: { read: { url: null, }, }, }, 如何检查dataSourceConfig.transport.read.url是否未定义?如果我正确理解您的问题,您必须检查所有中间对象是否存在: if (dataSourceConfig &&

我有一个目标

dataSourceConfig.transport.read.url

remoteDataSourceConfig: { 
transport: {
        read: {
            url: null, 
        },
    },
},

如何检查
dataSourceConfig.transport.read.url
是否未定义?

如果我正确理解您的问题,您必须检查所有中间对象是否存在:

if (dataSourceConfig &&
    dataSourceConfig.transport &&
    dataSourceConfig.transport.read &&
    dataSourceConfig.transport.read.url != null
) {
  // is not null or undefined
}
否则,您可以执行以下操作:

function isKeySet(obj, key) {
  var keys = key.split('.');
  return keys.reduce(function(o,k){ return o[k]; }, obj) != null;
}

isKeySet(dataSourceConfig, 'transport.read.url'); // false

如果我正确理解您的问题,您必须检查所有中介对象是否存在:

if (dataSourceConfig &&
    dataSourceConfig.transport &&
    dataSourceConfig.transport.read &&
    dataSourceConfig.transport.read.url != null
) {
  // is not null or undefined
}
否则,您可以执行以下操作:

function isKeySet(obj, key) {
  var keys = key.split('.');
  return keys.reduce(function(o,k){ return o[k]; }, obj) != null;
}

isKeySet(dataSourceConfig, 'transport.read.url'); // false

检查你知道javascript中可能有这个函数吗?我很确定没有内置函数,但是上面的
isKeySet
应该可以。你知道javascript中可能有这个函数吗?我很确定没有内置函数,但是上面的
isKeySet
应该可以。