下面,您可以看到这两个日志的输出。第一行代码清楚地显示了我试图访问的属性的完整对象,但在下一行代码中,我无法使用配置访问它。Col_id_3(见截图中的“undefined”?)有人能解释一下吗?我也可以访问除field_id_4之外的所有其他属性。

console.log(config);
console.log(config.col_id_3);

这就是这些行在控制台中打印的内容


当前回答

在我的情况下,它只是碰巧是,即使我接收到的数据在一个模型的格式,如myMethod(数据:MyModelClass)对象直到接收到的对象是类型字符串。 也就是console.log(data)中的y,我得到了内容。 解决方案就是解析JSON(在我的例子中)

const model:MyMOdelClass=JSON.parse(data);

思想可能是有用的。

其他回答

这可能会帮助某人,因为我有一个类似的问题,其中JSON.parse()返回一个对象,我可以在console.log()上打印,但我无法访问特定的字段,上述解决方案都不适合我。比如使用JSON.parse()和JSON.stringify()的组合。

var jsonObj = JSON.parse(JSON.stringify(responseText))

// where responseText is a JSON String returned by the server.

console.log(jsonObj) ///Was printing the object correctly
console.log(jsonObj.Body) /// Was printing Undefined  

我最终通过使用ExtJs Ext.decode()提供的不同解析器来解决这个问题;

var jsonObj = Ext.decode(responseText)
console.log(jsonObj.Body) //Worked...

console.log(anObject)的输出是误导性的;通过单击>展开控制台中显示的对象树,显示的对象的状态才会解析。当你console.log对象时,它不是对象的状态。

相反,尝试console.log(object .keys(config)),甚至console.log(JSON.stringify(config)),您将看到调用console.log时的键或对象的状态。

您将(通常)发现在console.log调用之后添加了键。

我也面临同样的问题。

从服务器,我在json_encode中返回数据。 像这样

 echo json_encode($res);  

当我返回一个不带json_encode的简单PHP数组后,问题就解决了。

return $res;

好吧,我经过了一个让我对新循环感到害怕的情况,感谢我的老派。

forEach循环和for(a in obj)循环都会产生错误的信息和错误,这取决于对象的类型和属性!!

老的好

for(var i=0, max=arr.length; i<max; i++)
{ //Properties available correctly!

   arr[i].property        //both work inside old school loop!
   arr[i][property]  
}             

我也遇到了同样的问题,但上面的解决方案对我来说都不奏效,之后我感觉就像是在猜测。但是,在setTimeout函数中包装创建对象的代码对我来说是有用的。

setTimeout(function() {
   var myObj = xyz; //some code for creation of complex object like above
   console.log(myObj); // this works
   console.log(myObj.propertyName); // this works too
});