0

I have a JSON structure as so:

[
    {
        "name": "Sam",
        "ride": "My Ride test",
        "session": "6htbgumjtmnxko6r",
        "info": null
    },
]

I wish to append data into the "info" part of the structure. my variables look like so:

var jsonData = [{                                   
    "name": "Sam",                                  
    "ride": "My Ride test",                         
    "session": session,                             
    "info": null
}];
jsonData.info = [];

I have tried:

jsonData.info.push({
        id: integer, 
        data:   { 
            distance: currentDistance || 0,
            lat: lat, 
            lon: lon
            } 
});
    jsonData.push(jsonData.info);

jsonData[3].push(jsonData.info);

All the above have resulted in is:

[
    {
        "name": "Sam",
        "ride": "My Ride test",
        "session": "6htbgumjtmnxko6r",
        "info": null
    },
    [
        {
            "id": 1,
            "data": {
                "distance": 0,
                "lat": 53.6419743,
                "lon": -1.7945115999999999
            }
        }
    ]
]

How would I go about pushing into the "info" section of my jsonData?

2 Answers 2

2
jsonData[0].info = {
    id: integer, 
    data:   { 
        distance: currentDistance || 0,
        lat: lat, 
        lon: lon
        } 
}

jsonData is an array, info is not a property of the array itself, just of the first value in the array.

If you wanted to add this to each value in array you would do something like:

jsonData.foreEach(function(v){ 
    v.info = {
        ...
    }
})

Where forEach is supported on all browsers > IE8.

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

1 Comment

Wow, wasn't thinking straight at all and completely missed that, thank you, because you answered first and provide a detailed answer I will mark your answer as correct, thanks.
2

You could try:

jsonData[0].info = {
        id: integer, 
        data:   { 
            distance: currentDistance || 0,
            lat: lat, 
            lon: lon
        } 
}

4 Comments

-1 Since it's the same answer as stackoverflow.com/a/22824437/2615737 and that one was posted first.
@FranciscoCorrales I appreciate it but he probably started typing his before mine was up.
@FranciscoCorrales Yeah he beat me by 14 seconds, there was no answer when i posted it.
Ok, I trust you, so no -1 for you. But the +1 goes to him.

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.