1

I do realize the title of this question is awkwardly chosen and I'll gladly change it towards something more descriptive if anybody has a suggestion, I just don't know how to describe it differently.

I have this bit of javascript code which is creating a json object containing an array labeled trips. Each object in this array contains an array labeled nodes, which I'd hope to contain objects with all the specific nodes of a trip.

var json = {origin: data.origin.name, destination: data.destination.name, trips: []};

for (var i = 0; i < data.trips.length; i++) {
    var departure = data.trips[i].dep.time;
    var arrival   = data.trips[i].arr.time;

    var trip = {departure: departure, arrival: arrival, nodes: []}

    for (var j = 0; j < data.trips[i].legs.length; j++) {
        trip.nodes.push({test: 'test'});
    }

    json.trips.push(trip);

}

The outcome looks like this.

{ origin: 'Dresden, Helmholtzstraße',
  destination: 'Dresden, Zellescher Weg',
  trips:
   [ { departure: '12:04',
       arrival: '12:26',
       nodes: [Object] },
     { departure: '13:02',
       arrival: '13:11',
       nodes: [Object] } ] }

The array I'm pulling the data from, the one the second for loop loops through, contains several elements. So I'd expect to see the test object a few times inside the nodes array. I don't quite understand how to interpret what I'm getting though. Is it an array containing an unspecified object? And if so, why?

8
  • 1
    How are you getting that output? Commented Aug 7, 2014 at 23:06
  • How do you mean how? Logging the json variable to the console? Commented Aug 7, 2014 at 23:07
  • In the browser? Are you running in node.js? console.log? Printing it in the repl prompt? Commented Aug 7, 2014 at 23:08
  • It's a node module I'm trying to build and it's required from another test file. The module itself however logs it to the console at the time being and it's run through node in my terminal. Commented Aug 7, 2014 at 23:09
  • 1
    its just going to print '[Object]' as thats what the .toString() method returns when logging to console. If you run in browser console you'll be able to navigate the object with a mouse. Commented Aug 7, 2014 at 23:10

1 Answer 1

4

I assume this is the output when you console.log the object. I believe nodes console.log function will fallback to just showing Object when the object is a certain depth. This makes sure your console isn't totally flooded by text.

To test it out try doing:

console.log(JSON.stringify(json, null, 4));

This will turn the json object into nicely formatted text, which should be logged perfectly.

Sign up to request clarification or add additional context in comments.

4 Comments

Fantastic, so it's been just fine. Thank you! And I spent the day pondering about this m)
console.log actually uses the util.inspect method under the hood when passed an object. If you call util.inspect directly you can path an explicit depth parameter (see the docs). JSON.stringify loses some data, so using util.inspect is a better way to see exactly what you're dealing with.
Cool, I didn't know about that. I wouldn't usually recommend stringifying an object because that'll break in a lot of cases. But it can be a good sanity check, like in this case.
THANK YOU SO MUCH

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.