如何通过键名访问对象值
我尝试了 x.y、x['y']、x.y.z、x['y'].z 的所有组合,但我无论如何都无法通过键名访问数组中对象值的值。以下是 SQL 查询返回的内容:
parseMissionData: {record: Array(1), code: 1}
code: 1
record: Array(1)
0: alt: 45
attribute: Array(6)
0: {name: "messageCode", value: "10"}
1: {name: "gpsFix", value: "2"}
2: {name: "autonomous", value: "0"}
3: {name: "lowBattery", value: "1"}
4: {name: "intervalChange", value: "30"}
5: {name: "resetDetected", value: "0"}
length: 6
__proto__: Array(0)
azimuth: 0
datetime: "2018-03-24T20:10:00.000Z"
devid: "300434060496300"
lat: 37.335394620895386
lon: -121.82005405426025
speed: 0
__proto__: Object
length: 1
__proto__: Array(0)
__proto__: Object
....
我无法保证属性始终以相同的顺序返回,因此我不能假设 attribute[0] 始终是 messageCode 属性。我尝试测试的是 messageCode 属性的值,以便我可以根据其值进行键入。我尝试了
response.record[i].attribute['messageCode']
response.record[i].attribute.message.value
response.record[i].attribute['messageCode'].value
和许多其他排列。有没有办法直接访问名为“messageCode”的属性,还是我必须遍历数组 属性并查找与名称值匹配的属性?
更新。我尝试了 Mathias247 的建议,现在的代码如下所示:
function parseMissionData( response ) {
console.log('parseMissionData: ', response);
var codeAttribute;
for ( var i=0; i < response.record.length; i++ )
codeAttribute = response.record[i].attribute.find(elem => elem.name == "messageCode");
console.log('messageCode: ', codeAttribute );
//console.log('codeAttribute: ', codeAttribute);
//console.log('imei via find: ', response.record[i].find(elem => elem.name == "devid"))
console.log('imei direct: ', response.record[i].devid);
}
使用更新后的代码,我现在可以像希望的那样看到 messageCode 的值。但奇怪的是,现在我对 imei (response.record[i].devid) 的引用已损坏并返回未定义!如果我注释掉 messageCode 的 find(elem =>...),response.record[i].devid 引用可以正常工作。我显然不明白使用 find 引用某些内容如何会破坏曾经有效的引用。有人能帮助我理解这里的相互作用吗?
messageCode: {name: "messageCode", value: "10"}
jquery.min.js:2 jQuery.Deferred exception: Cannot read property 'devid' of undefined TypeError: Cannot read property 'devid' of undefined
at parseMissionData (http://localhost:3000/js/controller.js:1036:67)
at Object.<anonymous> (http://localhost:3000/js/controller.js:1021:17)
at j (http://localhost:3000/js/jquery.min.js:2:29948)
at k (http://localhost:3000/js/jquery.min.js:2:30262) undefined
现在我更加困惑了。如果我按如下顺序排列控制台日志:
function parseMissionData( response ) {
console.log('parseMissionData: ', response);
var codeAttribute;
for ( var i=0; i < response.record.length; i++ )
console.log('imei direct: ', response.record[i].devid);
codeAttribute = response.record[i].attribute.find(elem => elem.name == "messageCode");
console.log('messageCode: ', codeAttribute );
//console.log('codeAttribute: ', codeAttribute);
//console.log('imei via find: ', response.record[i].find(elem => elem.name == "devid"))
}
如果我先记录 response.record[i].imei,输出正常,但现在 codeAttribute 会记录
jquery.min.js:2 jQuery.Deferred exception: Cannot read property 'attribute' of undefined TypeError: Cannot read property 'attribute' of undefined
at parseMissionData (http://localhost:3000/js/controller.js:1033:52)
at Object.<anonymous> (http://localhost:3000/js/controller.js:1021:17)
at j (http://localhost:3000/js/jquery.min.js:2:29948)
at k (http://localhost:3000/js/jquery.min.js:2:30262) undefined
如果我反转日志的顺序,属性会正常打印,但现在 imei 会记录未定义的错误。我肯定做错了什么,但我无论如何也想不出是什么问题。
do I have to iterate through the array attributes and look for a match to the name value?
是的,但您可以使用数组原型函数
find
轻松找到相关条目。例如
let codeAttribute = response.record[i].attribute.find(elem => elem.name == "messageCode")
// codeAttribute.value === '10'
我尝试重现您的问题,如下所示,并能够获取“messageCode”和“devid”的值。您能告诉我导致您出现此问题的具体原因吗:
a= {name: "messageCode", value: "10"}
b ={name: "gpsFix", value: "2"}
c = {name: "autonomous", value: "0"}
d = {name: "lowBattery", value: "1"}
e = {name: "intervalChange", value: "30"}
f = {name: "resetDetected", value: "0"}
attribute = [a,b,c,d,e,f]
record = { attribute, azimuth: '0', datetime: "2018-03-24T20:10:00.000Z", devid: "300434060496300",lat: '37.335394620895386'}
parseMissionData = {record, code: 1}
console.log(parseMissionData);
console.log(parseMissionData.record.attribute.find(o => o.name === 'messageCode').value);
console.log(parseMissionData.record.devid)