Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using _.some | _.any properly for lo-dash or underscore

I'm trying to see if any of the days are '01-01' ( the beginning of the year )

_.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.log like so .. :

_.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?

like image 917
Trip Avatar asked Jan 21 '13 22:01

Trip


People also ask

Why is JavaScript underscore better?

Underscore provides over 100 functions that support both your favorite workaday functional helpers: map, filter, invoke — as well as more specialized goodies: function binding, javascript templating, creating quick indexes, deep equality testing, and so on.

What is the function of underscore?

An underscore as the first character in an ID is often used to indicate an internal implementation that is not considered part of the API and should not be called by code outside that implementation.

What is underscore template?

The _. template() function is an inbuilt function in the Underscore. js library of JavaScript which is used to compile JavaScript templates into functions that can be evaluated for rendering. Useful for rendering complicated bits of HTML from JSON data sources.


1 Answers

You've misunderstood what the last argument to _.some is. The documentation shows 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.

You'll need to explicitly execute the equality test yourself.

_.some(a.days, function(day) {     return day.date.format('DD-MM') === "01-01"; }); 
like image 182
voithos Avatar answered Oct 20 '22 06:10

voithos