How to add values to a JSON object?
Posted
by Damiano
on Stack Overflow
See other posts from Stack Overflow
or by Damiano
Published on 2010-06-11T08:14:59Z
Indexed on
2010/06/11
8:22 UTC
Read the original article
Hit count: 261
JavaScript
|JSON
Hello everybody,
I have created an array with:
var msg = new Array();
then, I have a function that add values to this array, this function is:
function add(time, user, text){
var message = [time, user, text];
if (msg.length >= 50)
msg.shift();
msg.push(message);
}
As you can see, if the array has 50 or more elements I remove the first with .shift()
.
Then I add an array as element.
Ok, the code works perfectly, but now I have to loop the msg
array to create a JSON obj.
The JSON object should has this format:
var obj = [
{'time' : time, 'user' : user, 'text' : text},
{'time' : time, 'user' : user, 'text' : text},
{'time' : time, 'user' : user, 'text' : text}
]
I mean...i have to loop msg array and then store all the values inside the JSON object. I do not know how to "concatenate" the element of the array inside json obj.
Could you help me?
Thank you very much in advance!
© Stack Overflow or respective owner