Access object properties in Javascript

2019-03-06 02:19发布

问题:

I am trying to access email and password field but i dont know where this '0' came. I am retrieving object from rethinkdb and it looks good without '0'. But then am using Lodash _.assign() method like this

var user = new User
var finduser ={}
dbuser = finduser  //  dbuser is  the  object retrieving from db
user = _.assign(user,finduser)

I am getting data like this

{
    '0': {
        'email': 'email@ymail.com',
        'pswd': 'kdkd'
    }
}

I just want to access email field

回答1:

You can access like this,

user['0'].email

or

user['0']['email']



回答2:

you are retrieving array of data from database. That's how the 0 is coming. There should be a .first() method on db query or ORM you are using which will return single object of user not array.