简体   繁体   English

下划线_.each和_.map之间的区别是什么?

[英]What is really the difference between underscore _.each and _.map?

I'm using UnderscoreJs. 我正在使用UnderscoreJs。 Consider this code: 考虑以下代码:

var docs = [
    {name : 'Anders', niche : 'Web Development'}, 
    {name : 'Johnny', niche : 'Design'}, 
    {name : 'Eric', niche : 'PhotoShop'}
];

var newDocs = _.map(docs, function (doc){
    delete doc.niche;
    return doc;
});

It doesn't matter if I use .each or .map here. 如果我在这里使用.each.map并不重要。 The outcome is exactly the same. 结果完全一样。

What is really the difference between the two in the case above? 在上面的案例中,两者之间的真正区别是什么?

map is intended to be a functional mapping method: its function argument should return a value, but is not expected to have any side-effects . map旨在成为一种功能映射方法:它的函数参数应该返回一个值,但不会产生任何副作用

each is just a functional replacement for an imperative for loop: its purpose is to have an effect, and it is not expected to return any value . each只是for循环命令的功能替代:它的目的是产生效果,并且不会返回任何值

For example, this would be a more appropriate use for map : 例如,这对map更合适:

var docs = getDocs();
var docTitles = _.map(docs, function (doc){
    return doc.title;
});
// expect `docs` to be unchanged

Whereas this would be an appropriate use for each : 虽然这适用于each

var docs = getDocs();
_.each(docs, function (doc){
    delete doc.niche;
});
// expect `docs` to be altered.

_.each(list, iteratee) _.each(list,iteratee)

Iterates over a list of elements, yielding each in turn to an iteratee function. 迭代一系列元素,然后依次产生一个iteratee函数。

Each invocation of iteratee is called with three arguments: (element, index, list). 每次调用iteratee都会调用三个参数:(element,index,list)。 If list is a JavaScript object, iteratee's arguments will be (value, key, list). 如果list是JavaScript对象,则iteratee的参数将是(value,key,list)。 Returns the list for chaining. 返回链接列表。

_.each({one: 1, two: 2, three: 3}, alert);
=> alerts each number value in turn...

_.map(list, iteratee) _.map(list,iteratee)

Produces a new array of values by mapping each value in list through a transformation function (iteratee). 通过转换函数(iteratee)映射列表中的每个值,生成一个新的值数组。

If list is a JavaScript object, iteratee's arguments will be (value, key, list). 如果list是JavaScript对象,则iteratee的参数将是(value,key,list)。

_.map({one: 1, two: 2, three: 3}, function(num, key){ return num * 3; });
=> [3, 6, 9]

see documentation 文档

Your assertion that the results are "exactly the same" is untrue. 你断言结果“完全一样”是不真实的。 The _.each() function returns the original list, but _.map() returns a new list. _.each()函数返回原始列表,但_.map()返回一个列表。 You're directly modifying the original objects as you go, so you end up with references to the same objects in each list, but with _.map() you end up with two separate array instances. 您可以直接修改原始对象,因此最终会在每个列表中引用相同的对象,但是使用_.map()最终会得到两个单独的数组实例。

You can just look at the source code to see the difference: 您可以查看源代码以查看差异:

  • _.each : _.each

     _.each = _.forEach = function(obj, iteratee, context) { if (obj == null) return obj; iteratee = createCallback(iteratee, context); var i, length = obj.length; if (length === +length) { for (i = 0; i < length; i++) { iteratee(obj[i], i, obj); } } else { var keys = _.keys(obj); for (i = 0, length = keys.length; i < length; i++) { iteratee(obj[keys[i]], keys[i], obj); } } return obj; }; 
  • _.map : _.map

     _.map = _.collect = function(obj, iteratee, context) { if (obj == null) return []; iteratee = _.iteratee(iteratee, context); var keys = obj.length !== +obj.length && _.keys(obj), length = (keys || obj).length, results = Array(length), currentKey; for (var index = 0; index < length; index++) { currentKey = keys ? keys[index] : index; results[index] = iteratee(obj[currentKey], currentKey, obj); } return results; }; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM