Underscore 的相似函数:_.contains vs. _.some 和 _.map vs _.each

Underscore's similar functions: _.contains vs. _.some and _.map vs _.each

是否有理由使用一个而不是另一个? _.some 和 _.map 似乎更容易使用或适用于更多情况(根据我非常有限的经验),但从阅读它来看,它们听起来好像应该做同样的事情。我敢肯定还有其他这样的例子,我很乐意学习一些比较。

_.contains 对比 _.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.

_.some_.contains 之间的主要区别在于,contains 检查给定的项目是否存在于给定的列表中,而 some 检查是否有任何列表中的元素满足传递的谓词。所以,他们都在做不同的任务。

_.each 对比 _.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 简单地调用每个元素传递的函数 (iteratee)(注意:这不会创建数组)。

基本上 _.eachfor (var i = 0; i < array.length; i += 1) {...} 的等价物。同样,他们都在做不同的工作。

我认为@thefourtheye 的回答已经足够解释了,但我认为添加示例也可能有所帮助。

_.contains 对比 _.some

这里最大的区别是 _.contains 允许您提供一个值和一个索引,而 _.some 允许您提供一个谓词。

因此,例如,假设我们有一个包含前 10 个数字 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 的数组,我们如何检查数组 是否包含 数字 5?

我们可以使用_.contains函数:

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

我们也可以使用 _.some 函数,但它有点长:

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

但是,假设我们需要检查数组是否包含偶数?使用 _.contains 很难做到这一点,但是使用 _.some 你可以这样做:

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

所以是的,这里有区别,_.contains 主要用于检查数组是否包含特定元素,_.some 可用于检查数组是否包含具有特定元素的元素要求。

_.each 对比 _.map

这两个都是遍历整个数组,但是意思不同。使用 _.each 非常简单,您可以遍历数组:

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

另一方面,_.map 函数允许您 return 某些东西,它允许您 map 一个数组到一个新数组。假设您想要一个包含每个数字的数组,乘以 2,那么,您可以使用 _.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, ...

是的,您也可以使用 _.each 循环来完成此操作,方法如下:

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

但是 _.map 只是一个简短而简单的方法。

1) _.contains 对比 _.some

我认为,可以说 _.contains 是 _.some 的特例。 也就是说,

_.contains( list, value ) 

的快捷方式
_.some( list, function ( el ) { return value == 3; })

第二个变体太冗长了。

lodash中,许多函数中都有这样有用的快捷方式,例如:

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

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

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

2) _.map 对比 _.each

这些功能只是不同而已。 _.map 用于集合转换。如果输入的大小为 N,_.map 的结果也有 N 的大小。换句话说,_.map 是 expression - 在需要转换时使用它输入集合的每个元素。 _.each 只是 for 循环的快捷方式。这很有用,因为您不需要像

那样计算数组的长度
for (var i = 0, len = myArray.length; i < len; i++ ) {
}

当你正确使用_.map或_.each时,你就明确地表明了你的意图。