我对Underscore.js中的where()函数有疑问。 我有一个像这样的对象:
var location = [{ id: "1", name: "house 1", date: "20-11-2013" },
{ id: "2", name: "house 2", date: "30-11-2013"}];
一个表格作为过滤器使用像这样的选择:
From
<input type="date" name="from" />
Until
<input type="date" name="until" />
如何使where()函数返回日期大于From值且小于Until值的对象。
答案 0 :(得分:1)
我猜您无法使用where
执行此操作。您可以改为使用filter
doc :
var fromDate, untilDate;
fromDate = $('[name="from"]').val();
fromDate = fromDate.split('-');
fromDate = new Date(fromDate[0], fromDate[1] - 1, fromDate[2]);
untilDate = $('[name="until"]').val();
untilDate = untilDate.split('-');
untilDate = new Date(untilDate[0], untilDate[1] - 1, untilDate[2]);
var result = _.filter(location, function (item) {
var date;
date = item.date.split('-');
date = new Date(date[2], date[1] - 1, date[0]);
return date > fromDate && date < untilDate;
});