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

58,810

Solution 1

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";
});

Solution 2

You appear to be misunderstanding how to use _.some(). Consult the documentation and you'll see that your function needs to return true or false, and the last argument will be used as this in tat function.

You need to do this instead:

_.some(a.days,function(day){ return day.date.format("DD-MM") == "01-01";});
Share:
58,810

Related videos on Youtube

Trip
Author by

Trip

I program Ruby, C#, iOS, Node, and Augmented Reality for Unity3D. I write PostgreSQL, mySQL, SQLite, and MongoDB. I use Heroku, Amazon, Microsoft Azure. Creator of the Yoga Sutras App, Braidio Mobile, and Braidio. In my spare time, I teach Ashtanga Yoga. elephant trip AT gmail DOT com #happyToHelp

Updated on August 04, 2022

Comments

  • Trip
    Trip almost 2 years

    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?

    • Dean
      Dean over 4 years
      Btw if anyone came here inadvertently looking for the difference between 'some' and 'any', they are aliases and do the exact same thing.
  • Trip
    Trip over 11 years
    Thank you thank you thank you. This is absolutely correct. I could only choose one though so I went with the underdog :(