Access object properties in Javascript

Deadly 提交于 2019-12-04 06:25:57

问题


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.



来源:https://stackoverflow.com/questions/45628236/access-object-properties-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!