此代码返回一个字符串数组:
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
});