简体   繁体   English

遍历对象列表

[英]Iterating over list of objects

I have a list with lists with objects inside them, and i need to iterate over each object in all lists. 我有一个列表,列表中包含对象,并且我需要遍历所有列表中的每个对象。 This is what i have tried but doesnt work the way i intend it to. 这是我尝试过的方法,但没有达到我想要的方式。

dict_values([[<mapvis.store.Node object at 0x14fd370>, <mapvis.store.Node object at 0x13c4270>], [<mapvis.store.Node object at 0x14fd930>, <mapvis.store.Node object at 0x14fd490>], ])

Js function JS功能

function displayRoads(map) {
  {% for create_lat_lng in get_lat_lng %}
        {% for lat_lng in create_lat_lng %}
        var points = new google.maps.MVCArray();
        points.push(new google.maps.LatLng(lat_lng[0].lat, lat_lng[0].lng));
        points.push(new google.maps.LatLng(lat_lng[1].lat, lat_lng[1].lng));
        createPolyline(map, points);
  {% endfor %}
{% endfor %}
}

Html thats generated. HTML多数民众赞成在生成。 I want it to have actual values intstead of lat_lng[0].lng 我希望它具有实际值,而不是lat_lng [0] .lng

function displayRoads(map) {
    var points = new google.maps.MVCArray();
    points.push(new google.maps.LatLng(lat_lng[0].lat, lat_lng[0].lng));
    points.push(new google.maps.LatLng(lat_lng[1].lat, lat_lng[1].lng));
    createPolyline(map, points);
...
..
...
}

Use the {{ variable }} syntax. 使用{{ variable }}语法。

function displayRoads(map) {
  {% for create_lat_lng in get_lat_lng %}
      {% for lat_lng in create_lat_lng %}
        var points = new google.maps.MVCArray();
        points.push(new google.maps.LatLng({{ lat_lng.0.lat }}, {{ lat_lng.0.lng }}));
        points.push(new google.maps.LatLng({{ lat_lng.1.lat }}, {{ lat_lng.1.lng }}));
        createPolyline(map, points);
      {% endfor %}
  {% endfor %}
}

Just to clarify, {{ lat_lng.0.lat }} means access index zero on the lat_lng variable, and then get the value of the dictionary key lat . 为了澄清, {{ lat_lng.0.lat }}表示lat_lng变量的访问索引为零,然后获取字典键lat的值。 So the code above assumes lat_lng is a array/tuple containing dictionaries. 因此,上面的代码假定lat_lng是包含字典的数组/元组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM