我已经尝试了xy,x ['y'],xyz,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
....
我无法保证属性将始终以相同的顺序返回,因此我不能假设属性[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 =&gt; ...),则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记录了一个未定义的错误。我必须做错事,但不能为我的生活找出它是什么。
答案 0 :(得分:3)
我是否必须遍历数组属性并查找与名称值匹配的内容?
是的,但您可以使用数组原型函数find
轻松查找相关条目。 E.g。
let codeAttribute = response.record[i].attribute.find(elem => elem.name == "messageCode")
// codeAttribute.value === '10'
答案 1 :(得分:0)
我尝试重新创建ypur问题,如下所示,并且能够获得&#34; messageCode&#34;和&#34; devid&#34;。你能告诉我究竟是什么原因引起你的问题:
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)