nextProps 键虽然有内容但神秘地无法访问

nextProps keys mysteriously inaccessible though it has content

我在 react-redux 应用程序的 componentWillReceiveProps(nextProps) 中

console.log("Debug NextProps: ",nextProps.variantCategories);
console.log("Debug NextProps: ",Object.keys(nextProps.variantCategories));
//nextProps.variantCategories[1] returns undefined             <-- I need to access this

我为 Object.keys 得到了一个空数组,所以我认为我可能错误地访问了一些东西,并在一个全新的对象 i,

上进行了相同的测试
let i={1: {key: "value"}};
console.log("Debug i: ",i);
console.log("Debug i: ",Object.keys(i));

这是控制台(在右栏展开)

我做错了什么?

您看到的最有可能的情况是 属性 1 不可枚举,也就是说您不能以传统意义上的 属性.

如今大多数浏览器中的检查器会在控制台中显示 JavaScript 对象的所有方面,但这并不意味着您的代码将具有相同的权限或访问权限来检索该数据。要重现似乎正在发生的问题,您可以尝试以下代码片段:

var specialObject = Object.defineProperty({}, '1', {
    enumerable: false,
    value: 'some stuff here'
});

// console logging in an inspector would show the full contents
console.log(specialObject);
// prints: > Object {1: "some stuff here"}

// but trying to access the property as an enumerated value will not work:
console.log(Object.keys(specialObject));
// prints: > []

所以我的问题是 - 这些数据来自哪里?看来您期待的是更简单的对象。

我在应用程序开始以如此奇怪和神秘的方式做出反应之前改变了对象!

这可以通过检查在获取数据之前完成的控制台日志来找到,这些日志将保留当前值。