Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
67
rated 0 times [  70] [ 3]  / answers: 1 / hits: 29674  / 12 Years ago, sun, september 2, 2012, 12:00:00

I am getting a JSON reply, like following:



[{
order_id: 12,
customer: user user,
status: Pending,
date_added: 02/09/2012,
total: $500.00,
action: [{
text: View,
href: http://localhost/oc/admin/index.php?route=sale/order/info&token=92a80574e5fcbf3e2d021404cfaae1a4&order_id=12
}]
}]


have a look on action key, it's value is again an array. I am trying to get action key values by following code but it is showing undefined to me



function (data) {
if (data) {
for (var i = 0; i < data.length; i++) {
$('div.dashboard-content table.list tbody tr:first').before(
'<tr id=' +
data[i]['order_id'] +
'><td class=right>' +
data[i]['order_id'] +
'</td><td class=left>' +
data[i]['customer'] +
'</td><td class=left>' +
data[i]['status'] +
'</td><td class=left>' +
data[i]['date_added'] +
'</td><td class=right>' +
data[i]['total'] +
'</td><td class=right> [<a href=' +
data[i]['action']['href'] + '>' +
data[i]['action']['text'] +
'</a>]</td></tr>'
);
}
}
}


Can somebody help me.?
Thanks in advance.


More From » jquery

 Answers
13

Like you said, action is an array. Thus, you can't access it using data[i]['action']['href']. You have to use a subscript to indicate the position of the array that you want. For example, to access the first position, you'd use:



var href = data[i].action[0].href;
var text = data[i].action[0].text;

[#83288] Friday, August 31, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ramseydeshaunc

Total Points: 30
Total Questions: 91
Total Answers: 103

Location: Palau
Member since Tue, May 30, 2023
1 Year ago
;