Monday, December 22, 2008

Appending new items in JSON arrays with javascript

I decided to blog about this because it was a question that I struggled to solve and I couldn't find any material that gives the answer via google. To add a item to the end of a array in a JSON object, we need to take advantage of the length attribute of it. It is clearer to demonstrate this with code, like this piece of javascript:
json_data = {
    "some_array":[],
};

json_data.some_array[json_data.some_array.length] = new_item;

No comments: