使用 tumblr api json feed 获取标签和标签计数 [jquery]
我在使用 tumblr api 时遇到了一些棘手的问题。我目前正在获取所有帖子中的每个标签并将其存储在一个数组中。然而,这意味着如果一个数组被多次使用,它就会加倍。我希望能够只存储标签一次,并存储它出现的次数。有什么想法吗?我认为对象数组可以工作,但我正在努力弄清楚标签是否已放入数组中以及如何更新“计数”值。
var allTags = [];
var start = 0;
var cleanTags = [];
$(function() {
tumblrTag = function(tag,count) {
this.Tag = tag;
this.Count = count;
}
getTags()
});
function getTags() {
var tumblrApi = 'http://blog.rainbird.me/api/read/json?callback=?&num=50&start=' + start;
$.getJSON(tumblrApi, function (data) {
$(data.posts).each(function (i, post) {
$(post.tags).each(function (i, tag) {
if (typeof (tag) === 'string') {
newTag = new tumblrTag(tag, "1");
allTags.push(newTag);
}
});
});
if (start + 50 < data['posts-total']) {
start = start + 50;
getTags();
} else {
console.log("complete");
console.log(allTags);
}
});
}
I'm having a bit of a tricky problem with the tumblr api. I am currently getting every tag from all my posts and storing it in an array. However that means that if an array is used more than once it doubles up. I want to be able to just store a tag once only and also store how many times it's appeared. Any ideas? I was thinking an object array would work but I am struggling with working out if the tag has already been put into the array and how to update the "count" value.
var allTags = [];
var start = 0;
var cleanTags = [];
$(function() {
tumblrTag = function(tag,count) {
this.Tag = tag;
this.Count = count;
}
getTags()
});
function getTags() {
var tumblrApi = 'http://blog.rainbird.me/api/read/json?callback=?&num=50&start=' + start;
$.getJSON(tumblrApi, function (data) {
$(data.posts).each(function (i, post) {
$(post.tags).each(function (i, tag) {
if (typeof (tag) === 'string') {
newTag = new tumblrTag(tag, "1");
allTags.push(newTag);
}
});
});
if (start + 50 < data['posts-total']) {
start = start + 50;
getTags();
} else {
console.log("complete");
console.log(allTags);
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以这样做:
当你获得新标签时,使用类似的东西
You could do something like this:
and when you get new tags use something along the lines of