首页 > 解决方案 > 如何显示对象中的内容?

问题描述

我是 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.

标签: javascript

解决方案


使用最新的 chrome,然后console.log(obj)在控制台中输入。控制台将向您显示一个对象树,其中显示了所有属性和子属性。并非所有对象都可以字符串化,例如具有递归属性的对象。好的控制台还显示了节点的方法和dom树。


推荐阅读