如何使用.get()方法从javascript嵌套对象中获取属性



我有一个带有嵌套数组对象的嵌套对象。我需要分别提取每个对象。这是对象的示例:

records =  [{"keys":["voters","groups"],"length":2,"_fields":[{"identity":{"low":14,"high":0},"labels":["Voter"],"properties":{"name_key":"AINSWORTHLLOYDCECIL","occupation":"CARPENTER","gender":"MR","street":"12 ST JAMES ROAD","name":"LLOYD AINSWORTH","last_name":"AINSWORTH","middle_name":"CECIL","postal_code":"KGN 02","first_name":"LLOYD"}},{"identity":{"low":23666,"high":0},"labels":["Group"],"properties":{"name":"IMGroup","description":"Master group for individual members","since":{"low":-661343024,"high":374},"status":"active"}}],"_fieldLookup":{"voters":0,"groups":1}},{"keys":["voters","groups"],"length":2,"_fields":[{"identity":{"low":15,"high":0},"labels":["Voter"],"properties":{"name_key":"ALEXANDERDWAYNEBARRINGTON","occupation":"AIR CON TECH","gender":"MR","street":"3 1/2 JOHNSON TERRACE","name":"DWAYNE ALEXANDER","last_name":"ALEXANDER","middle_name":"BARRINGTON","postal_code":"KGN 02","first_name":"DWAYNE"}},{"identity":{"low":23666,"high":0},"labels":["Group"],"properties":{"name":"IMGroup","description":"Master group for individual members","since":{"low":-661343024,"high":374},"status":"active"}}],"_fieldLookup":{"voters":0,"groups":1}},{"keys":["voters","groups"],"length":2,"_fields":[{"identity":{"low":16,"high":0},"labels":["Voter"],"properties":{"name_key":"ALLENANNETTEUNA","occupation":"COSMETOLOGIST","gender":"MISS","street":"28 CARNARVAN STREET","name":"ANNETTE ALLEN","last_name":"ALLEN","middle_name":"UNA","postal_code":"KGN 02","first_name":"ANNETTE"}}........]

我习惯使用

records.map(obj => obj.get(0).properties ? obj.get(0).properties : obj.get(0));

records[0].get('voters').properties

出于某种原因,我得到了

obj.get is not a function

我不确定我在这里错过了什么。。。非常感谢您的帮助。

这是复制品:

records = [{
"keys": ["voters", "groups"],
"length": 2,
"_fields": [{
"identity": {
"low": 14,
"high": 0
},
"labels": ["Voter"],
"properties": {
"name_key": "AINSWORTHLLOYDCECIL",
"occupation": "CARPENTER",
"gender": "MR",
"street": "12 ST JAMES ROAD",
"name": "LLOYD AINSWORTH",
"last_name": "AINSWORTH",
"middle_name": "CECIL",
"postal_code": "KGN 02",
"first_name": "LLOYD"
}
}, {
"identity": {
"low": 23666,
"high": 0
},
"labels": ["Group"],
"properties": {
"name": "IMGroup",
"description": "Master group for individual members",
"since": {
"low": -661343024,
"high": 374
},
"status": "active"
}
}],
"_fieldLookup": {
"voters": 0,
"groups": 1
}
}, {
"keys": ["voters", "groups"],
"length": 2,
"_fields": [{
"identity": {
"low": 15,
"high": 0
},
"labels": ["Voter"],
"properties": {
"name_key": "ALEXANDERDWAYNEBARRINGTON",
"occupation": "AIR CON TECH",
"gender": "MR",
"street": "3 1/2 JOHNSON TERRACE",
"name": "DWAYNE ALEXANDER",
"last_name": "ALEXANDER",
"middle_name": "BARRINGTON",
"postal_code": "KGN 02",
"first_name": "DWAYNE"
}
}, {
"identity": {
"low": 23666,
"high": 0
},
"labels": ["Group"],
"properties": {
"name": "IMGroup",
"description": "Master group for individual members",
"since": {
"low": -661343024,
"high": 374
},
"status": "active"
}
}],
"_fieldLookup": {
"voters": 0,
"groups": 1
}
}, {
"keys": ["voters", "groups"],
"length": 2,
"_fields": [{
"identity": {
"low": 16,
"high": 0
},
"labels": ["Voter"],
"properties": {
"name_key": "ALLENANNETTEUNA",
"occupation": "COSMETOLOGIST",
"gender": "MISS",
"street": "28 CARNARVAN STREET",
"name": "ANNETTE ALLEN",
"last_name": "ALLEN",
"middle_name": "UNA",
"postal_code": "KGN 02",
"first_name": "ANNETTE"
}
}]
}]
console.log(records[0].get('voters').properties)

从外观上看,您可能熟悉Python,Python的字典对象包括一个.get方法。但是,JavaScript对象没有.get方法;它们的属性可以通过多种方式访问,但最常见的是使用点表示法:

records[0].voters.properties

在python中,当您试图访问的密钥不在字典中时,.get运算符通常用于防止错误。在JavaScript中,访问不属于对象的键时不会引发错误。相反,尝试访问对象中不存在的键只会导致值undefined

如果需要防止访问可能未定义的对象的属性(例如:records[0].voters.properties(,则可能需要使用可选的链接运算符?.,而不是通过.(例如:records[0].voters?.properties(进行的常规访问。


records = [{
"keys": ["voters", "groups"],
"length": 2,
"_fields": [{
"identity": {
"low": 14,
"high": 0
},
"labels": ["Voter"],
"properties": {
"name_key": "AINSWORTHLLOYDCECIL",
"occupation": "CARPENTER",
"gender": "MR",
"street": "12 ST JAMES ROAD",
"name": "LLOYD AINSWORTH",
"last_name": "AINSWORTH",
"middle_name": "CECIL",
"postal_code": "KGN 02",
"first_name": "LLOYD"
}
}, {
"identity": {
"low": 23666,
"high": 0
},
"labels": ["Group"],
"properties": {
"name": "IMGroup",
"description": "Master group for individual members",
"since": {
"low": -661343024,
"high": 374
},
"status": "active"
}
}],
"_fieldLookup": {
"voters": 0,
"groups": 1
}
}, {
"keys": ["voters", "groups"],
"length": 2,
"_fields": [{
"identity": {
"low": 15,
"high": 0
},
"labels": ["Voter"],
"properties": {
"name_key": "ALEXANDERDWAYNEBARRINGTON",
"occupation": "AIR CON TECH",
"gender": "MR",
"street": "3 1/2 JOHNSON TERRACE",
"name": "DWAYNE ALEXANDER",
"last_name": "ALEXANDER",
"middle_name": "BARRINGTON",
"postal_code": "KGN 02",
"first_name": "DWAYNE"
}
}, {
"identity": {
"low": 23666,
"high": 0
},
"labels": ["Group"],
"properties": {
"name": "IMGroup",
"description": "Master group for individual members",
"since": {
"low": -661343024,
"high": 374
},
"status": "active"
}
}],
"_fieldLookup": {
"voters": 0,
"groups": 1
}
}, {
"keys": ["voters", "groups"],
"length": 2,
"_fields": [{
"identity": {
"low": 16,
"high": 0
},
"labels": ["Voter"],
"properties": {
"name_key": "ALLENANNETTEUNA",
"occupation": "COSMETOLOGIST",
"gender": "MISS",
"street": "28 CARNARVAN STREET",
"name": "ANNETTE ALLEN",
"last_name": "ALLEN",
"middle_name": "UNA",
"postal_code": "KGN 02",
"first_name": "ANNETTE"
}
}]
}]
console.log(records[0].voters?.properties)

最新更新