Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.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中按外部日期属性对数组排序_Javascript_Node.js - Fatal编程技术网

JavaScript中按外部日期属性对数组排序

JavaScript中按外部日期属性对数组排序,javascript,node.js,Javascript,Node.js,我有一些.json文件要用NodeJs读取。这些文件包含日期、标题和内容 { "date": "10.06.2018", "title": "title goes here", "content": "content goes here" } 正如你所看到的,日期的格式是错误的,它是德语日期格式。读取目录时,我想按文件的日期属性对文件进行排序 目前我只是读取文件并尝试比较date属性 const path = 'articles'; // the path to sta

我有一些.json文件要用NodeJs读取。这些文件包含日期、标题和内容

{
    "date": "10.06.2018",
    "title": "title goes here",
    "content": "content goes here"
}
正如你所看到的,日期的格式是错误的,它是德语日期格式。读取目录时,我想按文件的日期属性对文件进行排序

目前我只是读取文件并尝试比较date属性

const path = 'articles'; // the path to start from

const directoryItems = fs.readdirSync(path); // get all the files from the directory

const articles = directoryItems.map(file => JSON.parse(fs.readFileSync(`${path}/${file}`))); // convert the files to objects

const sortedArticles = articles.sort((currentFile, otherFile) => currentFile.date < otherFile.date); // sort the objects by their date
const path='articles';//从哪里开始
const directoryItems=fs.readdirSync(路径);//从目录中获取所有文件
const articles=directoryItems.map(文件=>JSON.parse(fs.readFileSync(`${path}/${file}'));//将文件转换为对象
const sortedArticles=articles.sort((当前文件,其他文件)=>currentFile.date
我必须先将日期转换为有效的JavaScript日期格式吗?

您可以创建符合要求的日期,并使用字符串进行比较

函数de2iso(日期){
返回日期。替换(/(..)\(..)\(..)/,“$3-$2-$1”);
}
var数组=[{date:“11.06.2018”,title:“title goes here”,content:“content goes here”},{date:“10.06.2018”,title:“content goes here”,content:“content goes here”},{date:“content goes here”},{date date 01.02.2018”,title:“title goes here”,content:“content goes here”};
sort((a,b)=>de2iso(a.date).localeCompare(de2iso(b.date));
console.log(de2iso('10.06.2018'));
console.log(数组)您可以创建符合要求的日期并使用字符串进行比较

函数de2iso(日期){
返回日期。替换(/(..)\(..)\(..)/,“$3-$2-$1”);
}
var数组=[{date:“11.06.2018”,title:“title goes here”,content:“content goes here”},{date:“10.06.2018”,title:“content goes here”,content:“content goes here”},{date:“content goes here”},{date date 01.02.2018”,title:“title goes here”,content:“content goes here”};
sort((a,b)=>de2iso(a.date).localeCompare(de2iso(b.date));
console.log(de2iso('10.06.2018'));
console.log(数组)请尝试以下操作:

var arr=[{date:“10.06.2018”,“title:“title goes here”,“content:“content goes here”},{“date:“10.02.2018”,“title:“title goes here”,“content:“content goes here”}];
arr.sort(函数(a,b){
返回新日期(a日期)-新日期(b日期);
});
控制台日志(arr)请尝试以下操作:

var arr=[{date:“10.06.2018”,“title:“title goes here”,“content:“content goes here”},{“date:“10.02.2018”,“title:“title goes here”,“content:“content goes here”}];
arr.sort(函数(a,b){
返回新日期(a日期)-新日期(b日期);
});
控制台日志(arr)