Ну во-первых Ваш код асинхронный. Это значит, что в items будут значения только после того, как выполнится запрос.
То есть Ваш код должен будет выглядеть так:
var items = [];
jQuery.getJSON( url, function( json ) {
jQuery.each(json.posts, function(key, index) {
loc = index.custom_fields.map_coords[0];
items.push(loc);
});
console.log(items);
});
Во-вторых. Не факт что он у Вас выполниться - нужно добавить обработку ошибок:
var items = [];
jQuery.getJSON( url, function( json ) {
jQuery.each(json.posts, function(key, index) {
loc = index.custom_fields.map_coords[0];
items.push(loc);
});
console.log(items);
})
.fail(function() {
console.log( "error" );
})
Вопрос как работать с item дальше.
Вариантов несколько - через обещания или callback-функции.
Пример с callback-функцией.
var items = [],
callbacks = $.Callbacks();
callbacks.add(function () {
console.log(items);
})
jQuery.getJSON( url, function( json ) {
jQuery.each(json.posts, function(key, index) {
loc = index.custom_fields.map_coords[0];
items.push(loc);
});
callbacks.fire();
console.log(items);
})
.fail(function() {
console.log( "error" );
})
console.logзапрос к серверу ещё даже не отправился, потому что он асинхронный. Вы должны поставитьconsole.logсразу послеjQuery.each(...);– andreymal Mar 09 '17 at 12:36console.log, как мне использовать полученные данные потом. так же пихать всё послеeach? не дело же. – zagazat Mar 09 '17 at 12:38function(json){...}в отдельную функцию и там и пишите. – teran Mar 09 '17 at 12:40eachникто не запрещает – andreymal Mar 09 '17 at 12:42