当数据以不同的速率拉入时,如何将来自两个源的JSON数据存储在javascript对象中?
我正在尝试从使用ajax拉入的链接(使用jquery reddit api)以及关于链接的额外信息(从embed.ly jquery api拉入)创建当数据以不同的速率拉入时,如何将来自两个源的JSON数据存储在javascript对象中?,javascript,jquery,reddit,embedly,Javascript,Jquery,Reddit,Embedly,我正在尝试从使用ajax拉入的链接(使用jquery reddit api)以及关于链接的额外信息(从embed.ly jquery api拉入)创建文章对象 Article对象(包含来自RedditAPI的数据)存储在一个名为articles的数组中,使用push方法保留链接的顺序。我使用相同的I值来迭代reddit数据,并在articles数组中指定Article中的哪个Article来存储embed.ly数据。这是因为我在将embed.ly数据存储在错误的文章对象中时遇到了问题,因为emb
文章
对象
Article
对象(包含来自RedditAPI的数据)存储在一个名为articles
的数组中,使用push方法保留链接的顺序。我使用相同的I
值来迭代reddit数据,并在articles
数组中指定Article
中的哪个Article
来存储embed.ly数据。这是因为我在将embed.ly数据存储在错误的文章
对象中时遇到了问题,因为embed.ly需要花费大量时间才能获取数据
当为下一个子Reddit再次运行getRedditLinks
函数时,会产生问题。i
重置意味着extractEmbeddelyData
函数只从第一个子Reddit获取链接的embedded.ly数据。如果您运行代码()并查看控制台,您将看到我的意思-没有最近5篇文章的嵌入数据
因此,要确保这两组数据都存储在正确的文章
对象中,面临的挑战是,emed.ly不会以与reddit相同的速率提取数据
我曾尝试使用动态名称将每个子Reddit的项目数组存储在主关联数组/对象中,但收效甚微。这也许是可能的,但我还没想好怎么做
如果您知道如何在这种情况下创建动态命名的关联数组/对象,请您向我解释一下。或者,如果你能想出一个更好的解决方案,并愿意解释它,或者至少为我指出正确的研究方向,我将不胜感激
代码如下:
HTML:
JavaScript:
//Set embed.ly default key
$.embedly.defaults.key = 'a1206fe3b4214b3483b27db1a0a4f2e4';
//An array of subreddits which will eventually be generated from user input.
var userInput = ["nottheonion", "offbeat"]
//Iterate throught the userInputs, runnning the getRedditLinks function on each one.
for (var i = 0; i < userInput.length; i++) {
getRedditLinks(userInput[i]);
};
//Declare articles array where the Article objects will be stored
var articles = [];
//Article constructor
function Article(url, title, subreddit) {
this.url = url;
this.title = title;
this.description;
this.imgsrc;
this.rank;
this.subreddit = subreddit;
}
//Get Reddit URLs, titles and votes from a subreddit using JSON and store in new Articles. Push the Articles to the article array.
function getRedditLinks(subredditInput) {
$.getJSON(
"http://www.reddit.com/r/" + subredditInput + ".json?jsonp=?",
function foo(data) {
var ddc = data.data.children;
for (i = 0; i < 5 && i < ddc.length; i++) {
articles.push(new Article(ddc[i].data.url, ddc[i].data.title, ddc[i].data.subreddit));
//Run the embedly extract function on all the urls in the array.
extractEmbedlyData(articles[i].url, i)
}
console.log(articles);
});
//Extract embedly data and add to Articles.
function extractEmbedlyData(url, i) {
$.embedly.extract(url).progress(function (data) {
articles[i].description = data.description;
articles[i].imgsrc = data.images[0].url;
articles[i].rank = i;
});
}
}
//设置embed.ly默认密钥
$.embedly.defaults.key='a1206fe3b4214b3483b27db1a0a4f2e4';
//最终将由用户输入生成的子Reddit数组。
var userInput=[“nottheonion”,“offbeat”]
//迭代userInputs,在每个输入上运行getRedditLinks函数。
for(var i=0;i
只需将实际的文章
对象传递给提取嵌入数据
,而不是索引
for (i = 0; i < 5 && i < ddc.length; i++) {
var article=new Article(ddc[i].data.url, ddc[i].data.title, ddc[i].data.subreddit);
articles.push(article);
extractEmbedlyData(article)
}
function extractEmbedlyData(article) {
$.embedly.extract(article.url).progress(function (data) {
article.description = data.description;
article.imgsrc = data.images[0].url;
article.rank = i;
});
}
for(i=0;i<5&&i
这是一个非常优雅的答案。我所有的尝试都是矫枉过正。
for (i = 0; i < 5 && i < ddc.length; i++) {
var article=new Article(ddc[i].data.url, ddc[i].data.title, ddc[i].data.subreddit);
articles.push(article);
extractEmbedlyData(article)
}
function extractEmbedlyData(article) {
$.embedly.extract(article.url).progress(function (data) {
article.description = data.description;
article.imgsrc = data.images[0].url;
article.rank = i;
});
}