Im having a weird issue with json arrays.
Inside each array, there is another array called transactions.
It looks like so...
[{"account_name": "abc123",
"transactions": [
{"name": "1"},
{"name": "2"}
]},
{"account_name": "abc1234",
"transactions": [
{"name": "3"},
{"name": "4"}
]}
]
When I loop through the array, each element is recognized except for the transactions array.
Here is the loop that is supposed to send an alert foreach transactions array that is not empty.
for(var i = 0; i < accounts.length; i++)
{
var accountLine = "<tr><td class='bold'>" + accounts[i].account_name + "</td></tr>开发者_Python百科";
$("tbody#generalLedgerEntries").append(accountLine);
if(accounts[i].transactions.length < 0)
{
alert("we have transactions!");
for(var j = 0; j < accounts[i].transactions.length; j++)
{
var transLine = "<tr><td>" + accounts[i].transactions[j].type + "</td></tr>";
$("tbody#generalLedgerEntries").append(transLine);
}
}
}
Here is a working copy of the issue on jsfiddle...
http://jsfiddle.net/Ntrca/1/
Looks like you have your comparison operator backward to me. Should be:
if(accounts[i].transactions.length > 0)
When I make that change in the fiddle, I get the alert about transactions.
There is no "type" attribute of any of the stuff in your "transactions" arrays. Also, you check for the length for being less than zero instead of greater than.
change "type" to "name" and all works as intended...
精彩评论