Getting tags and tag count using the tumblr api json feed [jquery]
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']) {
开发者_StackOverflow社区 start = start + 50;
getTags();
} else {
console.log("complete");
console.log(allTags);
}
});
}
http://jsfiddle.net/k2UML/
You could do something like this:
obj[tag] = count;
and when you get new tags use something along the lines of
if( obj[newtag] ){
obj[newtag] += 1;
}else{
obj[newtag] = 1;
}
精彩评论