admin管理员组文章数量:1427134
I have a array for store object, which have an object in it already:
var obj = [{
name: 'json',
lang: 'en'
}];
console,.log(obj) //the result is OK;
then I want push another object into it, just like:
var newObj = {
name: 'lee',
lang: 'zh'
}
obj.push(newObj)
but after this I print the obj
array,console.log(obj)
, the result is 2
!!
Why this happen? How can I solve this problem?To store object in array correctly
I have a array for store object, which have an object in it already:
var obj = [{
name: 'json',
lang: 'en'
}];
console,.log(obj) //the result is OK;
then I want push another object into it, just like:
var newObj = {
name: 'lee',
lang: 'zh'
}
obj.push(newObj)
but after this I print the obj
array,console.log(obj)
, the result is 2
!!
Why this happen? How can I solve this problem?To store object in array correctly
Share Improve this question edited Aug 17, 2012 at 2:56 Chris Laplante 29.7k18 gold badges109 silver badges137 bronze badges asked Aug 17, 2012 at 2:52 hh54188hh54188 15.7k35 gold badges116 silver badges192 bronze badges 1-
1
The "obj" should be equivalent to
[{name: 'json', lang: 'en'},{name: 'lee', lang: 'zh'}]
after the described operations .. if not, that code is not a representative sample. – user166390 Commented Aug 17, 2012 at 2:56
1 Answer
Reset to default 5Make sure you didn't do obj = obj.push(newObj);
, because .push
method returns the number of elements after push; instead, the line should simply read obj.push(newObj)
.
本文标签: javascriptConfuse about array and object in nodejsStack Overflow
版权声明:本文标题:javascript - Confuse about array and object in node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745490494a2660582.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论