admin管理员组文章数量:1419666
If I have a blog post and I push ments with the line:
blogpostments.push({ username: "fred", ment: "Great"});
the ments section of JSON looks like this:
"ments":[{"0":{"username":"jim","ment":"Good",},"1":{"username":"fred","ment":"great"}}]
Ideally I'd like to see the JSON without the numerical additions ("0","1", etc) and flatter. Something like:
"ments":[{"username":"jim","ment":"Good"},{"username":"fred","ment":"great"}]
What do I need to change to get this?
If I have a blog post and I push ments with the line:
blogpost.ments.push({ username: "fred", ment: "Great"});
the ments section of JSON looks like this:
"ments":[{"0":{"username":"jim","ment":"Good",},"1":{"username":"fred","ment":"great"}}]
Ideally I'd like to see the JSON without the numerical additions ("0","1", etc) and flatter. Something like:
"ments":[{"username":"jim","ment":"Good"},{"username":"fred","ment":"great"}]
What do I need to change to get this?
Share Improve this question edited Jun 8, 2011 at 8:31 tooba asked Jun 7, 2011 at 18:27 toobatooba 5695 silver badges23 bronze badges 1- 2 How is that JSON generated? -i.e. where are you viewing it? – David Tang Commented Jun 8, 2011 at 3:12
1 Answer
Reset to default 3Wait, is blogpost.ments
a JavaScript array, or something else? If it were a JavaScript array, I don't see how executing the first line of code would update the JSON object as you described. I would expect it to automatically do what you expect, which is to push a new item on the end of the array.
In general, if you have an array blogpost.ments
, with this value:
[{"username":"jim","ment":"Good"}]
and you execute:
blogpost.ments.push({ username: "fred", ment: "Great"});
You will most certainly end up with blogpost.ments
having the value:
[{"username":"jim","ment":"Good"}, { "username": "fred", "ment": "Great"}]
Which leads me to believe that blogpost.ments
is not actually an array, but something else. You should give the code for blogpost.ments.push
if it is your own code.
So, basically ... make it an array, and it will work as you expect.
本文标签: javascriptHow to make a simple json array with pushStack Overflow
版权声明:本文标题:javascript - How to make a simple json array with push - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745292163a2651861.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论