简体   繁体   中英

Confuse about array and object in node.js

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

Make 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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM