数组包含元素,但长度=== 0

时间:2017-09-07 13:34:23

标签: javascript jquery

此代码返回一个字符串数组:

var getCitiesList = function (url, callback) {
    var citiesList = [];
    var search = function (needSearch,whereSearch) {
        var re = new RegExp(needSearch,'ig'),
            matched =  whereSearch.match(re); //возвращает массив совпадений
        return matched !== null;
    };
    $.getJSON(url)
        .done(function (data) {
            $.each(data, function (index, value) {
                if (search(input.val(), value.City)) {
                       citiesList.push(value.City);
                    }
            });
        });
    return citiesList;
};

当我在这里调用它时,它看起来像空数组,但包含元素array.length === 0

  input.keyup(function () {
    var cities = getCitiesList('../kladr.json');
    console.log(cities); //[] but contains elements
    console.log(cities.length);//0
});

How it looks in browser

0 个答案:

没有答案