简体   繁体   English

打印对象时JavaScript Google Chrome控制台中的错误

[英]Bug in JavaScript Google Chrome console when printing object

For some reason, printing an array of objects in console.log() doesn't print each object correctly. 由于某些原因,在console.log()中打印对象数组不能正确打印每个对象。

When I loop through the array for each individual object and print its property I am interested in, it differs from the object being printed in the array. 当我遍历每个对象的数组并打印我感兴趣的属性时,它与要在数组中打印的对象不同。 I performed this check before and after I print the array of objects with the check having matching property values while sometimes differing with the array. 我在打印对象数组之前和之后执行此检查,检查对象具有匹配的属性值,但有时与该数组有所不同。

What could be some reasons for this? 这可能是什么原因呢? I am happy to give more details but I'm not sure what is relevant to this problem. 我很乐意提供更多详细信息,但不确定与该问题有关的内容。

for(let i = 0; i < activeMonsters.length; i++) {
    console.log("Before: " + i + "|" + activeMonsters[i].xPos);
    // console.log(activeMonsters[i]);
}
console.log(activeMonsters);
for(let i = 0; i < activeMonsters.length; i++) {
    // console.log(activeMonsters[i]);
    console.log(i + "|" + activeMonsters[i].xPos);
}

It is not a bug, console.log() works as per design. 这不是错误, console.log()按照设计工作。 If I understand correctly you want to see Objects in details for that use: 如果我正确理解,则希望详细了解该用途的对象:

Option 1: 选项1:

console.dir() to print browserable Object in chrome console. console.dir()用于在chrome控制台中打印可浏览的对象。

For more information please check here : https://developer.mozilla.org/en-US/docs/Web/API/Console/dir 有关更多信息,请在此处检查: https : //developer.mozilla.org/en-US/docs/Web/API/Console/dir

Option 2: 选项2:

console.log(JSON.stringify());

This should also give you browserable Object. 这还应该为您提供可浏览的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM