多维 json 数组的问题......用小提琴
我对 json 数组有一个奇怪的问题。
在每个数组内,还有另一个称为事务的数组。
看起来像这样...
[{"account_name": "abc123",
"transactions": [
{"name": "1"},
{"name": "2"}
]},
{"account_name": "abc1234",
"transactions": [
{"name": "3"},
{"name": "4"}
]}
]
当我循环遍历数组时,除了交易数组之外,每个元素都会被识别。
以下循环应该为每个不为空的交易数组发送警报。
for(var i = 0; i < accounts.length; i++)
{
var accountLine = "<tr><td class='bold'>" + accounts[i].account_name + "</td></tr>";
$("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);
}
}
}
这是 jsfiddle 上问题的工作副本...
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>";
$("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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来你的比较运算符倒给了我。应该是:
当我在小提琴中进行更改时,我会收到有关交易的警报。
Looks like you have your comparison operator backward to me. Should be:
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...