I am new to JavaScript and would like to know how to display what is in an object I created.
This is my code:

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);

I thought the console.log(obj[]); statement would display it, but all it shows is object Object.

2 Answers

0
Danyal Imran On Best Solutions

The console.log(obj) should display a valid result

You could try stringifying the object as well

console.log(JSON.stringify(obj));

Another thing you could try it to open your chrome dev tools > console, and then type obj there, you can further inspect the key/value properties held by the object

0
vipcxj On

Use the latest chrome, then type console.log(obj) in the console. The console will show you a object tree which all properties and sub properties shown in it. Not all object can be stringified, such as the object with recursive property. The good console also show the method and dom tree of node.