Javascript 使用 _.some | _.any 适用于 lo-dash 或下划线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14448479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using _.some | _.any properly for lo-dash or underscore
提问by Trip
I'm trying to see if any of the days are '01-01' ( the beginning of the year )
我想看看是否有任何一天是“01-01”(年初)
_.some(a.days, function(day){ console.log(day.date.format('DD-MM')) }, "01-01")
Produces this array of dates in my console :
在我的控制台中生成这个日期数组:
01-01
02-01
03-01
04-01
05-01
06-01
07-01
So then I run without the console.loglike so .. :
然后我没有console.log像这样运行..:
_.some(a.days, function(day){ day.date.format('DD-MM') }, "01-01")
And it returns :
它返回:
false
Strange, eh? What do you think I'm doing incorrectly?
很奇怪吧?你认为我做错了什么?
回答by voithos
You've misunderstood what the last argument to _.someis. The documentationshows that it is the context, or scope, under which the iterator function runs, but it seems like you're trying to use it as a value for equality testing.
你误解了最后一个论点_.some是什么。该文件表明,它是context,或范围,迭代函数运行其下,但好像你正在尝试使用它作为平等的测试值。
You'll need to explicitly execute the equality test yourself.
您需要自己显式执行相等性测试。
_.some(a.days, function(day) {
return day.date.format('DD-MM') === "01-01";
});
回答by Niet the Dark Absol
You appear to be misunderstanding how to use _.some(). Consult the documentationand you'll see that your function needs to return trueor false, and the last argument will be used as thisin tat function.
您似乎误解了如何使用_.some(). 查阅文档,您将看到您的函数需要返回trueor false,并且最后一个参数将用作thistat 函数。
You need to do this instead:
你需要这样做:
_.some(a.days,function(day){ return day.date.format("DD-MM") == "01-01";});

