javascript - 如何显示对象中的内容?
问题描述
我是 JavaScript 新手,想知道如何显示我创建的对象中的内容。
这是我的代码:
function removeDuplicates(num) {
var x, // x is the index of the array
len = num.length,
out = [],
obj = {};
for (x = 0; x < len; x++) {
obj[num[x]] = 0;
console.log(x);
console.log(obj[num[x]]);
console.log(num[x]);
// first run [x] = 0
// obj[num[x]] = 0
// the push below writes out Ford
// second run x = 1
// obj[num[x]] = 0
// the push below overwrites the first Ford
// third run x = 2
// obj[num[x]] = 0
// the push below writes out GMC
// fourth run x = 3
// obj[num[x]] = 0
// the push below writes out Chevy
// fifth run x = 4
// obj[num[x]] = 0
// the push below overwrites the first Chevy
// etc
}
for (x in obj) {
console.log(obj[x]);
console.log("push loop");
console.log(obj);
out.push(x);
}
return out;
}
var theNum = ['Ford','Ford','GMC','Chevy','Chevy','Toyota','Ford'];
result=removeDuplicates(theNum);
console.log(theNum);
console.log(result);
我以为console.log(obj[]);
语句会显示它,但它显示的只是object Object
.
解决方案
使用最新的 chrome,然后console.log(obj)
在控制台中输入。控制台将向您显示一个对象树,其中显示了所有属性和子属性。并非所有对象都可以字符串化,例如具有递归属性的对象。好的控制台还显示了节点的方法和dom树。
推荐阅读
- python-3.x - PermissionError: [Errno13] 权限被拒绝
- automation - 如何在 JMeter 中添加响应和请求正文到邮件?
- reactjs - React 甚至设置了静态 defaultProps,但是报错为什么?
- python - UnicodeDecodeError:“ascii”编解码器无法解码位置 8 中的字节 0xc3:序数不在范围内(128)
- sql-server - Logstash 到 SQL Server 连接失败
- php - 滑块未以正确格式显示
- python - 如果日期时间缺少一分钟或更长时间的条目,则无法重新采样
- mysql - 两个表的where子句中的count()
- reactjs - 嵌套开关可以使用换页动画吗?
- c# - 如何让 ASP.NET Core 返回 XML 结果?