Underscore's similar functions: _.contains vs.

2019-04-14 21:13发布

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.

3条回答
干净又极端
2楼-- · 2019-04-14 21:45

_.contains vs _.some

_.contains (_.contains(list, value, [fromIndex])

Returns true if the value is present in the list. Uses indexOf internally, if list is an Array. Use fromIndex to start your search at a given index.

_.some (_.some(list, [predicate], [context]))

Returns true if any of the values in the list pass the predicate truth test. Short-circuits and stops traversing the list if a true element is found.

The main difference between _.some and _.contains is that, contains checks if a given item is present in the given list and some 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])

Iterates over a list of elements, yielding each in turn to an iteratee function.

_.map (_.map(list, iteratee, [context])

Produces a new array of values by mapping each value in list through a transformation function (iteratee).

_.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 of for (var i = 0; i < array.length; i += 1) {...}. Again, they both are doing different jobs.

查看更多
甜甜的少女心
3楼-- · 2019-04-14 21:49

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:

_.contains([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5); // True

We could use the _.some function as well, but it's a bit longer:

_.some([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function(num) {
  return num === 5;
}); // True

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:

_.some([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function(num) {
  return num % 2 === 0;
});

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:

_.each([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function(num) {
  console.log(num); // Will print each number
});

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:

var newCollection = _.map([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function(num) {
  return num * 2;
});
console.log(newCollection); // Will contain the numbers 2, 4, 6, 8, ...

Yes, you could do that with an _.each loop as well by doing something like this:

var newCollection = [];
_.each([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function(num) {
  newCollection.push(num * 2);
});
console.log(newCollection);

But _.map is just a short and easier way of doing that.

查看更多
不美不萌又怎样
4楼-- · 2019-04-14 22:02

1) _.contains vs _.some

I think, it is possible to say, that _.contains is special case of _.some. That is,

_.contains( list, value ) 

is a shortcut for

_.some( list, function ( el ) { return value == 3; })

The second variant is just too verbose.

In lodash such useful shortcuts presents in many functions, for example:

var users = [
    { 'user': 'barney', 'age': 36, 'active': true },
    { 'user': 'fred',   'age': 40, 'active': false }
];

_.contains({ 'user': 'fred', 'age': 40 }, 'fred');

or

_.filter(users, { 'age': 36 });

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

for (var i = 0, len = myArray.length; i < len; i++ ) {
}

When you use _.map or _.each properly, you declare explicitly your intentions.

查看更多
登录 后发表回答