JavaScript arrays have no keys. Use objects for this purpose.
var obj = {}; $.getJSON("displayjson.php",function (data) { $.each(data.news, function (i, news) { obj[news.title] = news.link; }); });
In JavaScript, objects act as associative arrays. Keep in mind that objects do not have a specific โsort orderโ when iterating over them (see below).
However . In your case, itโs not entirely clear to me why you generally transfer data from the original object ( data.news
). Why aren't you just passing a link to this object?
You can combine objects and arrays to achieve predictable iteration and key / value behavior:
var arr = []; $.getJSON("displayjson.php",function (data) { $.each(data.news, function (i, news) { arr.push({ title: news.title, link: news.link }); }); });
Tomalak Jan 28 '11 at 7:38 2011-01-28 07:38
source share