无法循环遍历javascript对象

无法循环遍历javascript对象,javascript,leaflet,geojson,Javascript,Leaflet,Geojson,我有一个对象区域边界,它是传单geojson对象 var district_boundary=new L.geoJson() 数据通过ajax调用添加到district的geojson文件中 $.ajax({ dataType: "json", url: "data/district.geojson", success: function(data) { $(data.features).each(function(key, data) { district_bounda

我有一个对象区域边界,它是传单geojson对象
var district_boundary=new L.geoJson()
数据通过ajax调用添加到district的geojson文件中

$.ajax({
dataType: "json",
url: "data/district.geojson",
success: function(data) {
    $(data.features).each(function(key, data) {
        district_boundary.addData(data);
        district_boundary.setStyle(district_boundary_styles["default"]);

    });
}
}))

因此在_层中有75个值,这些值的一个示例是图片中带有键149的值。 我需要遍历所有这75个值,并从features键获取值。i、 e地区名称,并用其标记地区。 我试过这个

_test = district_boundary._layers;
for (var aht in _test) {
   console.log('inside the loop');
   var b = _test[aht];
   console.log(b.feature.properties.NAME_3);
}
在控制台中进行尝试时,此操作可以正常工作,并显示结果 但是,当我在页面的脚本中运行时,这不起作用,尽管测试中填充了区域边界中的值。\u层,循环不会执行。为什么有人能帮我

msg和msg.d的输出为

我猜您的JSON数据被包装在一个
d
变量中。试试下面的方法-

$.ajax({
    dataType: "json",
    url: "data/district.geojson",
    success: function(msg) {
        //console.log(msg); //try this to see whether the data is really getting wrapped in d
        var data = msg.d; //msg.d contains the actual data
        $(data.features).each(function(key, data) {
            district_boundary.addData(data);
            district_boundary.setStyle(district_boundary_styles["default"]);        
        });
    }
});

解决了。除了执行流程之外,代码中的所有内容都很好。我试图使用稍后将根据流定义的数据,因此在脚本中我尝试循环通过_test的部分,它还没有填充,但当我在控制台中尝试时,它是在加载所有数据之后才开始工作的

否数据在
msg
中,不在
msg.d
中。无法读取未定义的属性“features”。我刚刚发现问题出在代码的执行流程上。不管怎样,谢谢扫描您将
console.log(msg)
的输出添加到您的问题中?我只是做了同样的事情:)