Kilian
Kilian

Reputation: 2282

Node array contains 'Object' instead of actual JS object

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?

Upvotes: 1

Views: 317

Answers (1)

nickclaw
nickclaw

Reputation: 698

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.

Upvotes: 4

Related Questions