Javascript 如何为过滤器使用具有多个参数的链和过滤器?
我正试图写一个函数来缩小基于日期部分的列表。 我在这里使用的库是下划线.js和矩.js 这是我到目前为止所拥有的Javascript 如何为过滤器使用具有多个参数的链和过滤器?,javascript,underscore.js,Javascript,Underscore.js,我正试图写一个函数来缩小基于日期部分的列表。 我在这里使用的库是下划线.js和矩.js 这是我到目前为止所拥有的 var data = [{adate: "2015-09-10T00:00:00-04:00", numUnits: 0}, {adate: "2015-09-11T00:00:00-04:00", numUnits: 2}]; //used in function below function filterByCurrentDatePart(item, datePart) {
var data = [{adate: "2015-09-10T00:00:00-04:00", numUnits: 0}, {adate: "2015-09-11T00:00:00-04:00", numUnits: 2}];
//used in function below
function filterByCurrentDatePart(item, datePart) {
console.log(item);
console.log(datePart);
return moment().isSame(item.adate, datePart);
};
//this is the function I call.
function filterListByCurrentDatePart(inputData, datePartType) {
return _.chain(inputData)
.filter(filterByCurrentDatePart(datePartType))
.value();
};
问题是datePartType被作为第一个参数输入。这在javascript中并不奇怪,但我认为chain函数会使其隐式化
如何使用两个函数使其工作?还是必须将它们组合起来,并在顶部使用一个变量?
//这是我调用的函数。
函数filterListByCurrentDatePart(inputData,datePartType){
var now=moment();//出于性能原因缓存它
返回链(输入数据)
.过滤器(功能(项目){
立即返回.isName(item.adate,datePartType)
})
.value();
};
风险值数据=[{
谚语:“2015-09-10T00:00:00-04:00”,
货币:0
}, {
谚语:“2015-09-19T00:00:00-04:00”,
货币:2
}];
log(filterListByCurrentDatePart(数据'd'))代码>
在filterListByCurrentDatePart()中反转形式参数,然后在filterListByCurrentDatePart()中使用.filter(filterByCurrentDatePart.bind(null,datePartType))
!你能把它作为答案转寄给我吗?