This code returns an array of strings:
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;
};
When I call it here, it is looks like empty array, but contains elements, array.length === 0
input.keyup(function () {
var cities = getCitiesList('../kladr.json');
console.log(cities); //[] but contains elements
console.log(cities.length);//0
});