javascript - 从嵌套数组中获取数据

标签 javascript jquery

如何从该 JavaScript 对象中的每个 markers 获取 routes 数组中的每个 id,同时仍然引用 item.id :

{
    "markers": [
        {
            "id": "77475",
            "smsCode": "77475",
            "name": "Abbey Sports Centre",
            "stopIndicator": "Y",
            "towards": "Goodmayes or Upney",
            "direction": "sw",
            "lat": 51.53472971807365,
            "lng": 0.07973349341716908,
            "routes": [
                {
                    "id": "62",
                    "name": "62"
                },
                {
                    "id": "287",
                    "name": "287"
                },
                {
                    "id": "368",
                    "name": "368"
                },
                {
                    "id": "387",
                    "name": "387"
                },
                {
                    "id": "687",
                    "name": "687"
                }
            ]
        }
    ]
}

实际结果中有更多标记,为了节省空间而缩短了。这是我的 jQuery。

$.each(data.markers, function(i,item){
        $.each(item.routes, function(i,routes){   
            $('<span>').html(routes.id + " ").appendTo("#p_" + item.id);         
            //alert("#p_" + item.id + " " +routes.id);
        });

$('<li>').html(
    "<a href=#_" + item.id +" onclick=getBusListingForStop(" + item.id + ");><h2>" + item.name + " (Stop " + item.stopIndicator + ") to " + item.towards + "</h2><p id='p_" + item.id + "'>Buses: " + item.lat + " " + item.lng + "</p></a>"
).appendTo('#stopListing');

});

示例: http://jsbin.com/uniyar/2/edit

更新

愚蠢的是,代码顺序错误,我在创建实际元素之前运行了第二个循环。重新订购并且工作正常。

$.each(data.markers, function(i,item){        
  
  $('<li>').html(
    "<a href=#_" + item.id +" onclick=getBusListingForStop(" + item.id + ");><h2>" + item.name + " (Stop " + item.stopIndicator + ") to " + item.towards + "</h2><p id='p_" + item.id + "'>Buses:</p></a>"
).appendTo('#stopListing');
  
  $.each(item.routes, function(i,routes){   
            $('<span>').html(routes.id + " ").appendTo("#p_" + item.id);         
            //alert("#p_" + item.id + " " +routes.id);
        });
});

最佳答案

愚蠢的是,代码顺序错误,我在创建实际元素之前运行了第二个循环。重新订购并且工作正常。

$.each(data.markers, function(i,item){        

  $('<li>').html(
    "<a href=#_" + item.id +" onclick=getBusListingForStop(" + item.id + ");><h2>" + item.name + " (Stop " + item.stopIndicator + ") to " + item.towards + "</h2><p id='p_" + item.id + "'>Buses:</p></a>"
).appendTo('#stopListing');

  $.each(item.routes, function(i,routes){   
            $('<span>').html(routes.id + " ").appendTo("#p_" + item.id);         
            //alert("#p_" + item.id + " " +routes.id);
        });
});

Works Fine

关于javascript - 从嵌套数组中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16316676/

相关文章:

javascript - ajax 相当于这个curl命令

javascript - 分页,循环函数内的变量

jquery - 当文本框内容可编辑为假时如何更改文本颜色?

javascript - Actionscript 3 - 如何删除菜单栏、滚动条,只显示一个空白弹出窗口?

javascript - 我无法在我的 asp 下拉列表中找到所选值

jquery - 显示[]条目数据表更改位置

jquery - 从中心向外扩展元素,忽略 div 边界

javascript - pretty-print 在推特 Bootstrap 中不起作用

javascript - Backbone.js:url 路由末尾的尾部斜杠

javascript - 跨多台机器的 Cypress [Azure Devops]