admin管理员组文章数量:1241094
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!
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!
Share Improve this question asked Jun 11, 2010 at 8:14 DamianoDamiano 711 gold badge1 silver badge2 bronze badges 1-
2
It's generally better to initialize array like this
var msg = [];
instead ofvar msg = new Array();
– RaYell Commented Jun 11, 2010 at 8:21
3 Answers
Reset to default 10I'll give you an example from your add function:
function add(time, user, text){
// this line is all I changed
var message = {'time' : time, 'user' : user, 'text' : text};
if (msg.length >= 50)
msg.shift();
msg.push(message);
}
As you can see the message variable is no longer an array but it's the Object you want it to be.
From this you should be able to work out how to create a new array and add the values you want to it.
Try this:
var len = msg.length;
var obj = [];
for (var i = 0; i < len; i++) {
var item = {
'time': msg[i][0],
'user': msg[i][1],
'text': msg[i][2]
}
obj.push(item);
}
I think you want something like this:
function add(time, user, text){
var message = {time:time, user:user, text:text};
if (msg.length >= 50)
msg.shift();
msg.push(message);
}
本文标签: javascriptHow to add values to a JSON objectStack Overflow
版权声明:本文标题:javascript - How to add values to a JSON object? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739995416a2219320.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论