UnderscoreJS - 从对象内的对象中检索特定键

时间:2017-12-06 18:57:47

标签: underscore.js

我有以下对象,我试图从token

中检索UserEmailDetails
var obj = {
  "id": null,
  "firstName": null,
  "lastName": null,
  "createdAt": "2016-10-05T18:16:07.000Z",
  "updatedAt": "2016-10-05T18:16:07.000Z",
  "Details":
    {
      "id": 1,
      "token": null,
      "deviceId": null,
      "code": 12345678,
      "verified": null,
      "createdAt": "2016-10-05T18:16:07.000Z",
      "updatedAt": "2016-10-05T18:16:07.000Z",
      "UserEmail": "joe@example.com"
    }
 }

我尝试了这个,但我得到了一个空白?

_.pick(_.pick(obj, 'Details'), 'code', 'UserEmail');

3 个答案:

答案 0 :(得分:1)

您的内部pick会返回一个带有Details键的对象,然后您必须访问该对象。相反,做一些像:

_.pick(obj['Details'], 'code', 'UserEmail')

答案 1 :(得分:1)

_.pick(obj.Details, 'code', 'UserEmail')

答案 2 :(得分:0)

如果您在对象中严格设置了详细信息键,那么我会看到以下方式:

class PersonListAdapter(realm: Realm) :
        RealmExpandableListAdapter<String, Person>(
                itemGroupsProvider = { person -> arrayOf(person.group, null) },
                groupsProvider = { people -> people.map { it.group } },
                adapterData = realm.where(Person::class.java)
                        .findAllSortedAsync("lastName", Sort.ASCENDING, "firstName", Sort.ASCENDING)) {

    override fun getGroupId(groupPosition: Int) = getGroup(groupPosition).id

    override fun getChildId(groupPosition: Int, childPosition: Int) = getChild(groupPosition, childPosition).id

    override fun hasStableIds() = true

    override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup?): View {
        // ... Item View here ...
    }

    override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean,
                              convertView: View?, parent: ViewGroup?): View {
        // ... Group View here ...
    }

    override fun isChildSelectable(groupPosition: Int, childPosition: Int) = true
}