多维 json 数组的问题......用小提琴

发布于 2024-10-28 01:21:24 字数 1180 浏览 1 评论 0原文

我对 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 上问题的工作副本...

http://jsfiddle.net/Ntrca/1/< /a>

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...

http://jsfiddle.net/Ntrca/1/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流绪微梦 2024-11-04 01:21:24

看起来你的比较运算符倒给了我。应该是:

if(accounts[i].transactions.length > 0)

当我在小提琴中进行更改时,我会收到有关交易的警报。

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.

如梦亦如幻 2024-11-04 01:21:24

“交易”数组中的任何内容都没有“类型”属性。此外,您还检查长度是否小于零而不是大于。

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.

七堇年 2024-11-04 01:21:24

将“类型”更改为“名称”,一切按预期工作......

change "type" to "name" and all works as intended...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文