JQuery将我的非空数组视为空?

时间:2016-09-06 20:21:35

标签: javascript jquery arrays

很简单,我已经在一段很长的脚本中构建了一个多维数组,最后我想循环遍历每个级别并做一些事情。我已经在控制台中记录了数组并且可以看到应有的一切,但是控制台然后告诉我数组长度为0并且确定如果我.length它也告诉我长度为0,所以我的JQuery每个都没有被解雇。我无法理解人们可以做些什么才能让它像这样。我绝对难过!

这是控制台: enter image description here

这是JQuery,虽然数组/对象是建立在如此多的脚本和ajax调用之上的,我试图只把它放入计数......

var field_layers = [];

function blah_blah() {
    do_api_call("api_call_url_here")
        .done(function(response, textStatus, xhr) {
            var map_layers = response;
            $(map_layers).each(function(map_layers_key, map_layers_detail) {
                var datalayer_id = map_layers_detail.Layer.DataLayerId;

                // add field ids to array:
                field_layers["field_id_"+map_layers_detail.Layer.FieldId] = [];
                do_api_call("api_call_url_here/"+datalayer_id)
                    .done(function(response, textStatus, xhr) {
                        // create an empty array for field layers:
                        field_layers["field_id_"+map_layers_detail.Layer.FieldId]["layer_id_"+datalayer_id] = [];
                        var map_layer_zones = response;
                        $(map_layer_zones).each(function(map_layer_zone_key, map_layer_zone_detail) {
                            // Add the zones to the layer
                            field_layers["field_id_"+map_layers_detail.Layer.FieldId]["layer_id_"+datalayer_id].push({
                                "zone_id":zone.DataLayerZoneId,
                                "title":zone.DataLayerZoneId+" title here"
                            });
                        });
                    });
            });
        });
}

function go_to_field(field_id) { // this is what gives me the console screenshot
    console.log("field_id: "+field_id);
    console.log(field_layers["field_id_"+field_id]);
    console.log(field_layers["field_id_"+field_id].length);
}

1 个答案:

答案 0 :(得分:1)

尝试将field_layers = []更改为对象而不是数组:field_layers = {}。我想如果你改变每个地方你正在初始化一个空数组来初始化一个空对象,那么你的代码应该按原样运行。

注意方括号和花括号之间的区别。

您来自PHP背景,您可以将Javascript对象({})视为PHP中的关联(或命名)数组。

所以最终的代码如下:

var field_layers = {};

function blah_blah() {
    do_api_call("api_call_url_here")
        .done(function(response, textStatus, xhr) {
            var map_layers = response;
            $(map_layers).each(function(map_layers_key, map_layers_detail) {
                var datalayer_id = map_layers_detail.Layer.DataLayerId;

                // add field ids to object:
                field_layers["field_id_"+map_layers_detail.Layer.FieldId] = {};
                do_api_call("api_call_url_here/"+datalayer_id)
                    .done(function(response, textStatus, xhr) {
                    var map_layer_zones = response;
                    $(map_layer_zones).each(function(map_layer_zone_key, map_layer_zone_detail) {
                        // Add the zones to the layer
                        field_layers["field_id_"+map_layers_detail.Layer.FieldId]["layer_id_"+datalayer_id] = {
                            "zone_id":zone.DataLayerZoneId,
                            "title":zone.DataLayerZoneId+" title here"
                        }; // notice the assignment of object literal here
                    });
                });
        });
    });
}

function go_to_field(field_id) { // this is what gives me the console screenshot
    console.log("field_id: "+field_id);
    console.log(field_layers["field_id_"+field_id]);
    console.log(field_layers["field_id_"+field_id].length);
}

我还没有对此进行测试,但我认为它应该可行。