Is there a reason to use one over the other? It seems that _.some and _.map are easier to use or applicable to more situations (from my very limited experience) but from reading it, they sound as if they should do the same thing. I'm sure there are other instances of this and I'm all ears on learning some of the comparisons.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
_.contains
vs_.some
_.contains
(_.contains(list, value, [fromIndex])
_.some
(_.some(list, [predicate], [context]))
The main difference between
_.some
and_.contains
is that,contains
checks if a given item is present in the given list andsome
checks if any of the elements in the list satisfies the predicate passed. So, they both are doing different tasks._.each
vs_.map
_.each
(_.each(list, iteratee, [context])
_.map
(_.map(list, iteratee, [context])
_.map
calls the function passed (iteratee
) with each and every element of the list to create a new Array object, but_.each
simply calls the function passed (iteratee
) with each and every element (Note: this doesn't create an Array).Basically
_.each
is the functional equivalent offor (var i = 0; i < array.length; i += 1) {...}
. Again, they both are doing different jobs.I think @thefourtheye's answer explains enough, but I thought that adding an example may help as well.
_.contains
vs_.some
The big difference here is that
_.contains
allows you to provide a value and an index, while_.some
allows you to provide a predicate.So, for example, let's say we have an array with the first 10 numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
, how do we check if the array contains the number 5?We can use the
_.contains
function:We could use the
_.some
function as well, but it's a bit longer:But, let's say we need to check if the array contains any even number? It's hard to do that with
_.contains
but with_.some
you could do something like this:So yeah, there's a difference here, while
_.contains
is primarily used to check if an array contains a specific element,_.some
could be used to check if an array contains an element with specific requirements._.each
vs_.map
Both these loop through the entire array, but they both have a different meaning. With
_.each
it's quite simple, you can loop through an array:The
_.map
function on the other hand allows you to return something, it allows you to map an array to a new array. Let's say you want an array of every number, multiplied by 2, well, you can do that with_.map
:Yes, you could do that with an
_.each
loop as well by doing something like this:But
_.map
is just a short and easier way of doing that.1) _.contains vs _.some
I think, it is possible to say, that _.contains is special case of _.some. That is,
is a shortcut for
The second variant is just too verbose.
In lodash such useful shortcuts presents in many functions, for example:
or
2) _.map vs _.each
These functions are just different. _.map used for collection transformation. If your input has size of N, result of _.map also has size of N. In other words, _.map is expression - use it when you need to transform each element of input collection. _.each is just a shortcut for the for loop. It is useful, because you don't need to calculate length of array as in
When you use _.map or _.each properly, you declare explicitly your intentions.