【发布时间】:2021-03-24 01:09:25
【问题描述】:
我希望过滤掉 2000 年到 2021 年之间的记录。
"Rank": 50,
"Title": "Casablanca",
"Description": "A cynical nightclub owner protects an old flame and her husband from Nazis in Morocco.",
"Runtime": 102,
"Genre": "Drama",
"Rating": 8.5,
"Metascore": "100",
"Votes": 441864,
"Gross_Earning_in_Mil": "1.02",
"Director": "Michael Curtiz",
"Actor": "Humphrey Bogart",
"Year": 1942
我尝试使用从其他解决方案中找到的方法,但它无法正常工作。
var startDate = new Date("1990");
var endDate = new Date("2000");
var resultProductData = movies.filter(a => {
var date = new Date(a.Year);
return (date >= startDate && date <= endDate);
});
【问题讨论】:
-
const result = movies.filter(({year}) => (year > 2000 && year < 2021));year属性是整数,直接查看即可。 -
您的过滤器不起作用的原因是您正在从字符串中生成
startDate和endDate日期,并且从整数中生成比较日期。字符串被读取为年,而整数 js 解释为自纪元时间以来的毫秒数。
标签: javascript json filter