$(document).ready( function() { app.initialized() .then(function(_client) { var client = _client; client.events.on('app.activated', function() { client.data.get('ticket') .then(function(data) { $('#issue_title').text("Issue:" + data.ticket.description); }) .catch(function(e) { console.log('Exception - ', e); }); }); }); });
但当我把它改成数组对象时,它就不起作用了
$(document).ready( function() { app.initialized() .then(function(_client) { var client = _client; client.events.on('app.activated', function() { client.data.get('ticket') .then(function(data) { $('#issue_title').text("Issue:" + data.ticket.attachments['name']); }) .catch(function(e) { console.log('Exception - ', e); }); }); }); });
我正在使用这个票证附件负载
Sample Payload { "ticket": { "attachments": [], "cc_emails": [], "company_id": 1, "created_at": "2017-04-12T06:05:56.000Z", "custom_fields": [{ "custom_number": null, "custom_line1": " " }], }
现在,您试图像对象一样导航数组,数组是基于索引的,没有键:)
{ "ticket": { "attachments": { name: "", }, "cc_emails": [], "company_id": 1, "created_at": "2017-04-12T06:05:56.000Z", "custom_fields": [{ "custom_number": null, "custom_line1": " " }], }
如果它是一个结构为[key,value,key,value]的数组,您可以
data.ticket.attachments[data.ticket.attachments.indexOf('name') + 1]
最后,如果它是一个对象数组,你可以去
data.ticket.attachments.find(attachment => attachment.key === 'name').YourKey