javascript—使用';财产';in对象失败

javascript—使用';财产';in对象失败,javascript,Javascript,我有一个名为this.props的对象: { children: null, location: { action: 'POP', query: { seconds: '300' } } } 在查看之后,我编写了以下代码: let seconds = 0; console.log(this.props); if('seconds' in this.props) { seconds = parseInt(this.props.location

我有一个名为this.props的对象

{
  children: null,
  location: {
    action: 'POP',
    query: {
      seconds: '300'
    }
  }
}
在查看之后,我编写了以下代码:

let seconds = 0;

console.log(this.props);

if('seconds' in this.props) {
 seconds = parseInt(this.props.location.query.seconds, 10);
}

console.log(seconds);
在控制台中,对象如上所述记录,但
seconds
记录为0


我不明白我的if检查有什么问题,即使嵌套对象具有正确的属性,条件似乎也失败了(问题是,当没有查询对象时,整个this.props对象是空的-因此我需要检查嵌套属性)。

秒不在this.props中;正在查询中。
中的
运算符不是递归的,只是第一级:

if('seconds' in this.props.location.query) {
 seconds = parseInt(this.props.location.query.seconds, 10);
}

console.log(seconds); // 300
如果要递归执行,请创建自己的函数:

let isIn = function (obj, key) {
  return isObj(obj) ? (key in obj) || Object.values(obj).filter(f => isIn(f, key)).length > 0 : false;
}
其中,
isObj
是验证参数是否为对象的函数。您可以使用lodash或等效物,或制作如下内容:

let isObj = t => (t !== null) && (typeof t === 'object');

那么,如果存在嵌套对象,那么检查嵌套对象的正确方法是什么呢?我的问题是,如果我的URL中没有任何参数,那么this.props只是一个空对象-{}我添加了一个示例:)