2013-08-28 16 views
0

的文件夾中它以特定的格式,即MODEL_RELEASEDATE命名智能手機從使用JQuery

文件夾中的閱讀與最新的日期一個文件,我已經有一個文件夾,在文件下面的名字

文件名

SmartphoneA_11122012 
SmartphoneA_01022013 
SmartphoneA_09102013 
SmartphoneA_10072012 
SmartphoneA_12042012 
**SmartphoneB_08282013** 
SmartphoneB_04152013 
SmartphoneB_08282012 
SmartphoneB_01062013 
. 
. 
. 
. 
and so on 

我想寫一個jQuery的代碼,我可以使用特定的關鍵字,從格式,從上面的列表中,我將通過價值SmartphoneA,我應該能夠讀取與最新的發佈日期的文件。與通過關鍵字智能手機B時一樣。

如果我通過K/W SmartphoneB,結果應該從文件送達上文所強調的,即SmartphoneB_08282013

我當前的代碼讀取文件名只與特定的K/W上。我幾乎沒有任何改動。

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script> 
<script type="text/javascript"> 
    var metaKeywords=$('meta[name=keywords]').attr("content");//this utility works based upon the keywords on the pages. 
    var reqdKeyword = new Array(); 
    reqdKeyword = metaKeywords.split(","); 
    var randKeyword = reqdKeyword[Math.floor(Math.random() * reqdKeyword.length)]; 
    var cdnUrl = "http://abc.com/xyz/mobiles/"; 
    var jsnUrl = ".json?callback=showDetail"; 
    var finalUrl= cdnUrl.concat(randKeyword.trim()).concat(jsnUrl); 


    /* 

      Rest of the code goes here 

    */ 


</script> 

回答

0

您將需要一個服務器端的代碼來回報你的URI(文件名)的列表,那麼你就可以編寫JavaScript代碼來分析它(但在這種情況下,如果你的服務器端可能會更好代碼將根據查詢字符串立即返回正確的名稱)。在最糟糕的情況下,您可以在服務器上放置一個dir.txt文件,該文件將列出該文件夾中的所有文件,例如運行cron作業以根據需要更新它。

jQuery將無法列出服務器上的遠程文件,除非您的服務器以某種方式支持它。

更新

一旦你的文件,你需要:

一)令牌化它到一個數組,例如這樣

var names = dir.split("\n"); 

b)只留下開頭的字符串關鍵字和關鍵字切斷

names = $(names).map(function(n,i) { 
    return (n.indexOf(keyword) == 0) ? n.split('_')[1] : null; 
}).get(); 

現在你有一個這樣的數組[ '11122012', '01022013',...]

三)發現這個數組

var dateNum = Math.max.apply(null, 
    $.map(names,function(n,i){ return parseInt(
    n.replace(/(\d{2})(\d{2})(\d{4})/, function(match, month, day, year) { 
      return year + month + day; 
     })) 
    })); 
var maxDate = dateNum.toString().replace(/(\d{4})(\d{2})(\d{2})/, 
     function (match, year, month, day) { return month + day + year; } 
     ); 
var fileName = keyword + "_" + maxDate; 

瞧在最大日期,你的文件名中包含有最大日期名稱。

還有其他的方法,例如,真的把日期解析成Date對象。此外,您可以簡單地迭代文件一次,而不使用數組映射和Math.max()迭代器。由於代碼量在這裏勝過速度,因此要找到最佳的代碼取決於您可以在哪裏重新使用它的零件和零件而不會影響可維護性。

http://jsfiddle.net/Exceeder/VLB2E/

+0

我有一個服務器端代碼,將放置在上述格式的文件。我只需要以名稱中的最新日期讀取文件。 –

+0

我更新了我的答案。我建議你給你的問題添加一個更新,這樣來這個問題的人就會明白你已經擁有了文件列表以及它是一個文本字符串還是JSON數組或者其他的東西。 –

+0

我已更新問題,謝謝,並且您的更新似乎很有用。如有需要,我會盡快回復您。再次感謝。 –

1

約日期很酷的事情是,如果你在「降」命令(即,年,月,日,小時,秒)的日期,你可以很容易地對它們進行排序。利用這一點,我們就可以通過你的文件抓住只是用正確的前綴開頭的那些,然後輕鬆地獲取最新的一個:

var filenames = [ 
     'SmartphoneA_11122012', 
     'SmartphoneA_01022013', 
     'SmartphoneA_09102013', 
     'SmartphoneA_10072012', 
     'SmartphoneA_12042012', 
     'SmartphoneB_08282013', 
     'SmartphoneB_04152013', 
     'SmartphoneB_08282012', 
     'SmartphoneB_01062013' 
    ], 
    whichPhone = 'SmartphoneB', // Dummy value, this would come from user interaction or whatever 
    possibleFiles = []; 

// This goes through your list of filenames and picks out just the ones that match `whichPhone`, then puts them into another array containing a "cleaned-up" date and some other metadata-esque stuff 
for (var i = 0, j = filenames.length; i < j; i++) { 
    var filename = filenames[i]; 

    if (filename.indexOf(whichPhone) > -1) { 
     possibleFiles.push({ 
      index: i, 
      filename: filename, 
      date: parseInt(filename.split('_')[1].replace(/(\d{2})(\d{2})(\d{4})/, function(match, month, day, year) { 
       return year + month + day; 
      }), 10) 
     }); 
    } 
} 

// Now we go through the `possibleFiles` and figure out which one has the latest date 
var latestFileDate = 0, 
    theActualFilenameYouWantFinally; 

for (var i = 0, j = possibleFiles.length; i < j; i++) { 
    var possibleFile = possibleFiles[i]; 

    if (possibleFile.date > latestFileDate) { 
     latestFileDate = possibleFile.date; 
     theActualFilenameYouWantFinally = filenames[possibleFile.index]; 
    } 
} 

// And, finally, your result 
console.log(theActualFilenameYouWantFinally); 

編輯:我沒有爲這個答案使用jQuery因爲咩,你對於這樣的事情並不需要jQuery。不要誤會我的意思,John Resig非常出色,幾乎所有的東西都使用jQuery,但for循環很快就會很容易處理,而且這樣的東西無論如何都不是真正的jQuery強大的套裝。

+0

這是從我見過的DDMMYYYY日期中取出整數的最簡單方法。我會爲此+1。但我實際上乘以10的冪作爲2012 + 12 + 31超過2013 + 1 + 1 –

+0

您不必乘法,因爲20,121,231小於20,130,101(這是您從該代碼中得到的結果)。這是魔術! –

+0

當然你是對的。不知何故,我錯過了你添加ymd作爲字符串,而不是整數。 –