Reputation: 693
I have tried a couple of methods using indexOf and if (x in y) none of which seem to do exactly what I want.
I need to pull through several objects and find a specific number / timestamp / Date depending on the object. Here is a screenshot of the console of my Object.
Any thoughts?
Specifically when I first get the data I extract all the Timestamps (posted on date / time) from the API Response(s). Using that Array of timestamps I am trying to find the most recent posted object from all the responses.
Here are some more screen captures:
Twitter created_at
timestamp
Tumblr timestamp
and my dates array
And my code used to pull the information and sort it.
//AJAX CALLS
$.when(
//Bitter Syndrome
$.ajax({
type: "GET",
dataType: "jsonp",
cache: false,
url: "http://api.tumblr.com/v2/blog/"+bittersymdrome_tumblr+"/posts/?api_key="+tumblr_apiKey,
success: function(data){
delete data.meta; delete data.response.blog;
blogs.content.push(data);
}
}),
//moundsMusic Tumblr
$.ajax({
type: "GET",
dataType: "jsonp",
cache: false,
url: "http://api.tumblr.com/v2/blog/"+moundsmusic_tumblr+"/posts/?api_key="+tumblr_apiKey,
success: function(data){
delete data.meta; delete data.response.blog;
blogs.content.push(data);
}
}),
//GateWayDrugSTL Tumblr
$.ajax({
type: "GET",
dataType: "jsonp",
cache: false,
url: "http://api.tumblr.com/v2/blog/"+gatewaydrug_tumblr+"/posts/?api_key="+tumblr_apiKey,
success: function(data){
delete data.meta; delete data.response.blog;
blogs.content.push(data);
}
}),
//GateWayDrugSTL Twitter
$.ajax({
type: "GET",
dataType: "jsonp",
cache: false,
url: "http://api.twitter.com/1/statuses/user_timeline.json?user_id="+gatewaydrug_twitter,
success: function(data){
blogs.content.push( data );
/*var time = data[0].created_at;
gwdtwDate = Date.parse(time)/1000;*/
}
})
).done( function(){
//Get and Sort Dates most recent first.
for(var i=0;i<blogs.content.length;i++){
if(!blogs.content[i].length){
for(var e=0; e<blogs.content[i].response.posts.length; e++){
blogs.dates.push(blogs.content[i].response.posts[e].timestamp);
}
} else {
for (var e = 0; e<blogs.content[i].length; e++){
var time = blogs.content[i][e].created_at;
gatewaydrug_twitter_date = Date.parse(time)/1000;
blogs.dates.push(gatewaydrug_twitter_date);
}
}
}
blogs.dates.sort(function(a,b){return b-a});
console.log( blogs );
for(var i=0,len=blogs.dates.length; i<len;i++){
}
});
Here is a live link to a test server where you can view the object in your browsers console: http://thehivestl.com/test/
Upvotes: 0
Views: 479
Reputation: 665040
Don't mix different object types in the same array, but normalize them before. Also, you should not need to use a global blogs
variable, instead use the Deferreds properly.
function handleTumblrResponse(data) {
delete data.meta; // actually, you don't
delete data.response.blog; // need those
var resultdates = [];
for (var e=0; e<data.response.posts.length; e++)
resultdates.push(data.response.posts[e].timestamp);
return resultdates;
}
function handleTwitterResponse(data) {
var resultdates = [];
for (var e = 0; e<data.length; e++){
var timesting = data[e].created_at;
var date = Date.parse(time)/1000;
resultdates.push(date);
}
return resultdates;
}
$.when(
// those get… functions are the plain ajax calls, no success handlers
getBitterSyndromeAjax().then(handleTumblrResponse),
getMoundsMusicTumblr().then(handleTumblrResponse),
getGateWayDrugSTLTumblr().then(handleTumblrResponse),
getGateWayDrugSTLTwitter().then(handleTwitterResponse)
).done(function(bitterSyndrom, moundsMusic, drugsTumb, drugsTwit) {
// concat the four arrays to one
var dates = Array.prototype.concat.apply([], arguments);
dates.sort(function(a,b){return b-a});
// do something with the timestamps
});
Btw, I'm quite sure Date.parse cannot handle that odd Twitter format. Instead, use
function fromDateString(str) {
var res = str.match(/(\S{3})\s(\d{2})\s(\d+):(\d+):(\d+)\s(?:([+-])(\d\d)(\d\d)\s)?(\d{4})/);
if (res == null)
return NaN; // or something that indicates it was not a DateString
var time = Date.UTC(
+res[9],
{"Jan":0,"Feb":1,"Mar":2, …, "Dec":11}[res[1]] || -1,
+res[2],
+res[3],
+res[4],
+res[5],
);
if (res[6] && res[7] && res[8]) {
var dir = res[6] == "+" ? -1 : 1,
h = parseInt(res[7], 10),
m = parseInt(res[8], 10);
time += dir * (h*60+m) * 60000;
}
return time;
}
Upvotes: 1