0

I'm trying to push JSON data to an array within an array. The problematic difference to other examples of this I can find is that both arrays are being built by a loop which I believe is the reason for the error I'm receiving. TypeError: Cannot call method 'push' of undefined

Here's a somewhat minimal example of what I'm trying to achieve.

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;

    json.trips.push({departure: departure, arrival: arrival, nodes: []});

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

The output I'm trying to create should be looking like this.

{
    origin: origin,
    destination: destination,
    trips: [
        {
            departure: departure,
            arrival: arrival,
            nodes: [
                {test: test},
                {test: test},
                {test: test}
            ]
        },
        {
            departure: departure,
            arrival: arrival,
            nodes: [
                {test: test},
                {test: test},
                {test: test}
            ]
        }
    ]
}

The test nodes are nonsensical, sure, but shouldn't adding them in this way be possible?

1
  • you need to hold temporary instance of the trip object, push node objects into the node array in the temporary trip object, then push the temporary trip object into the trip array in the json object. Commented Aug 6, 2014 at 23:19

3 Answers 3

2

The line:

json.trips.nodes.push({test: 'test'});

should be:

json.trips[i].nodes.push({test: 'test'});
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, so a simple syntax error was the culprit behind the type error. At least fixes the error, but the problem persists in a slightly different form. Don't quite know if I should edit the question accordingly or not as it does go into a different direction. Thanks though :)
If it's another question, you should post another question.
0

json.trips.nodes is indeed undefined. I believe you want to add it to the new item in the trip loop?

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

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

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

1 Comment

This is not very reusable, and it would be hard to adjust for future use.
0
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);

}

2 Comments

Thanks for the example and especially the comment above. But for a reason unbeknownst to me the nodes aren't being created as they should be. This just results in nodes: [Object] for every trip. Any ideas?
My suspicion is the call to "json.trips.nodes" is trying to access the trips object instead of the array and creating a node member. It is treating the trips member as an object.

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.