Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用JQuery从文件夹中读取具有最新日期的文件_Javascript_Jquery_Arrays_Sorting - Fatal编程技术网

Javascript 使用JQuery从文件夹中读取具有最新日期的文件

Javascript 使用JQuery从文件夹中读取具有最新日期的文件,javascript,jquery,arrays,sorting,Javascript,Jquery,Arrays,Sorting,我已经有了一个文件夹,其中的文件以特定格式命名,即MODEL_RELEASEDATE 名为智能手机的文件夹中的文件名 SmartphoneA_11122012 SmartphoneA_01022013 SmartphoneA_09102013 SmartphoneA_10072012 SmartphoneA_12042012 **SmartphoneB_08282013** SmartphoneB_04152013 SmartphoneB_08282012 SmartphoneB_0106201

我已经有了一个文件夹,其中的文件以特定格式命名,即MODEL_RELEASEDATE

名为智能手机的文件夹中的文件名

SmartphoneA_11122012
SmartphoneA_01022013
SmartphoneA_09102013
SmartphoneA_10072012
SmartphoneA_12042012
**SmartphoneB_08282013**
SmartphoneB_04152013
SmartphoneB_08282012
SmartphoneB_01062013
.
.
.
.
and so on
我想写一个jquery代码,在这里我可以使用格式中的特定关键字,从上面的列表中,我将传递值SmartphoneA,我应该能够读取最新发布日期的文件。与传递关键字SmartphoneB时的情况相同

如果我通过了k/wSmartphoneB,结果应该来自上面突出显示的文件,即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>

var metaKeywords=$('meta[name=keywords]')。attr(“content”)//此实用程序根据页面上的关键字工作。
var reqdKeyword=新数组();
reqdKeyword=metaKeywords.split(“,”);
var randKeyword=reqdKeyword[Math.floor(Math.random()*reqdKeyword.length)];
变量cdnUrl=”http://abc.com/xyz/mobiles/";
var jsnUrl=“.json?callback=showDetail”;
var finalUrl=cdnUrl.concat(randKeyword.trim()).concat(jsnUrl);
/*
代码的其余部分在这里
*/

您需要一个服务器端代码来返回URI(文件名)列表,然后您可以编写JavaScript代码来解析它(但在这种情况下,如果您的服务器端代码根据查询字符串立即返回正确的名称,可能会更好)。在最坏的情况下,您可以在服务器上放置一个dir.txt文件,该文件将列出该文件夹中的所有文件,例如,运行cron job以根据需要进行更新

除非您的服务器以某种方式支持jQuery,否则jQuery将无法列出服务器上的远程文件

更新

SmartphoneA_11122012
SmartphoneA_01022013
SmartphoneA_09102013
SmartphoneA_10072012
SmartphoneA_12042012
**SmartphoneB_08282013**
SmartphoneB_04152013
SmartphoneB_08282012
SmartphoneB_01062013
.
.
.
.
and so on
获得所需文件后:

a) 将其标记为数组,例如

 var names = dir.split("\n"); 
b) 只保留以关键字开头的字符串,并将关键字截断

 names = $(names).map(function(n,i) { 
    return (n.indexOf(keyword) == 0) ?  n.split('_')[1] : null;
 }).get();
现在有了这样一个数组['11122012','01022013',…]

c) 在此数组中查找最大日期

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;
瞧,你的文件名包含了一个带有最大日期的名字

还有其他方法,例如,将日期解析为日期对象。此外,您可以简单地迭代文件一次,而无需数组映射和Math.max()迭代器。在这里,代码的数量胜于速度,找到最佳的代码取决于在不影响可维护性的情况下重用代码的位置


日期最酷的一点是,如果您将日期按“降序”(即年-月-日-小时-秒)排序,则可以轻松地对日期进行排序。使用该功能,我们可以浏览您的文件,仅获取以正确前缀开头的文件,然后轻松获取最新的文件:

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);
var文件名=[
“智能电话A_11122012”,
“智能电话A_01022013”,
“智能电话A_09102013”,
“智能电话A_10072012”,
“智能电话A_12042012”,
“SmartphoneB_08282013”,
“智能电话B_04152013”,
“SmartphoneB_08282012”,
“SmartphoneB_01062013”
],
whichPhone='SmartphoneB',//Dummy值,这将来自用户交互或其他
可能文件=[];
//这将遍历您的文件名列表,只选择与“whichPhone”匹配的文件名,然后将它们放入另一个包含“清理”日期和其他元数据类型内容的数组中
for(var i=0,j=filenames.length;i-1){
可能推({
索引:i,,
filename:filename,
日期:parseInt(filename.split(''u')[1]。替换(/(\d{2})(\d{2})(\d{4})/,函数(匹配,月,日,年){
返回年+月+日;
}), 10)
});
}
}
//现在我们浏览一下“可能文件”,找出哪一个文件的日期最晚
var latestFileDate=0,
您最终想要的实际文件名;
for(var i=0,j=possibleFiles.length;ilatestFileDate){
latestFileDate=possibleFile.date;
您想要的实际文件名最终=文件名[possibleFile.index];
}
}
//最后是你的结果
console.log(您最终想要的实际文件名);

编辑:我没有使用jQuery来回答这个问题,因为meh,对于这样的问题,您实际上不需要jQuery。别误会,John Resig非常出色,我几乎在所有事情中都使用jQuery,但是
for
循环非常快速且易于使用,而且像这样的东西无论如何都不是jQuery的强项。

我有一个服务器端代码,可以将文件以上述格式放置。我只需要阅读文件名中有最新日期的文件。我更新了我的答案。我建议您在问题中添加一个更新,以便回答此问题的人了解您已经有了文件列表,以及它是一个文本字符串还是JSON数组或其他内容。我已经更新了问题,谢谢,您的更新似乎很有用。如果需要的话,我会给你回电话的。再次感谢。您好@Alex Pakka,我的文件夹的最终名称如下,您的代码没有解析它,因为日期格式不同且不一致(如果日期/月份小于10,则前面没有“0”),因此无法工作。你能在fiddle更新代码吗?smartphoneA_2005-3-1、smartphoneA_2007-3-13、smartphoneA_2008-3-4、smartphoneA_2009-11-17、smartphoneA_2010-11-2、smartphoneB_2010-3-16、smartphoneB_2012-8-28、smartphoneB_2013-3-12、smartphoneC_2014-3-12、smartphoneC_2014-9-13、smartphoneC_2011-3-15、smartphoneC_2004-11-2、smartphoneC_2007-11-20文件名是从数据库中呈现的,所以我对日期没有太多的控制这是我见过的从DDMMYYYY日期中提取整数的最短方法。那我就+1。但实际上我会把ymd乘以o的幂