我有这个剥离的代码:
var dateFormat = ' *some date format* ';
var unixFormat = ' *the unix date format* '
function sorter(date, key) {
var result = date.sort(function(a, b) {
var date1 = a.key;
var date2 = b.key;
console.log(key); //returns "publishedDate"
console.log(date1); //returns undefined
var testDate = a.publishedDate;
console.log(testDate); //returns *some date*
var first = moment(date1, dateFormat).format(unixFormat);
var second = moment(date2, dateFormat).format(unixFormat);
return second - first;
});
return result;
};
var data = ' *array with entries such as "publishedDate: *some date*"* ';
var sorted = sorter(data, 'publishedDate');
我试图将变量date1和date2设置为正确的值。在这种情况下,那将是" a.publishedDate"和" b.publishedDate"。
" PublishedDate"名称的一部分不是常数,所以我试图将其传递给函数,正如您在变量" sorted"中看到的那样。
但是当我这样做时,我只是未定义。有什么想法吗?