0

我已经尝试了几种使用 indexOf 和 if (x in y) 的方法,这些方法似乎都没有完全符合我的要求。

我需要遍历几个对象并根据对象找到特定的数字/时间戳/日期。这是我的对象控制台的屏幕截图。

有什么想法吗? 在此处输入图像描述

特别是当我第一次获取数据时,我从 API 响应中提取所有时间戳(发布在日期/时间)。使用该时间戳数组,我试图从所有响应中找到最新发布的对象。

以下是更多屏幕截图:

推特created_at时间戳 Twitter created_at 时间戳

棒棒哒timestamp Tumblr 时间戳

和我的日期数组 在此处输入图像描述

我的代码用来提取信息并对其进行排序。

//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++){

    }
});

是一个测试服务器的实时链接,您可以在浏览器控制台中查看该对象:http: //thehivestl.com/test/

4

1 回答 1

1

不要在同一个数组中混合不同的对象类型,而是先规范化它们。此外,您不需要使用全局blogs变量,而是正确使用 Deferreds。

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
});

顺便说一句,我很确定 Date.parse 无法处理那种奇怪的 Twitter 格式。相反,使用

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;
}
于 2012-12-04T20:52:47.213 回答