Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Coffeescript _.每个vs.map有什么区别?_Coffeescript_Underscore.js - Fatal编程技术网

Coffeescript _.每个vs.map有什么区别?

Coffeescript _.每个vs.map有什么区别?,coffeescript,underscore.js,Coffeescript,Underscore.js,我试着把名单上所有的动物都打印出来。我试着使用。每种都很好用。但是当我使用\uuu.map时,它们的结果是相同的 代码: animals = [ "dog","cat","pig" ] 使用.map: _.map animals, (animal)-> console.log " " + animal //result: dog cat pig 使用\每个: _.each animals, (animal)-> console.log " " + ani

我试着把名单上所有的动物都打印出来。我试着使用
。每种
都很好用。但是当我使用
\uuu.map
时,它们的结果是相同的

代码:

animals = [ "dog","cat","pig" ]
使用
.map

_.map animals, (animal)->
    console.log " " + animal

    //result: dog cat pig
使用
\每个

_.each animals, (animal)->
    console.log " " + animal

    //result: dog cat pig
问题:

  • 这两者有什么区别
  • 这两者的主要/超级功能是什么

  • 我是JavaScript新手,我试着阅读了,但我不理解其中的一些术语。

    。每个
    都只是一个for循环,为每个元素执行给定的函数

    \uuz.map
    为每个元素收集给定函数的返回值,并按顺序返回所有返回值的列表

    如果放弃
    .map
    的结果(如示例中所示),则其操作与
    \u相同,但会浪费一些内存


    因此,从功能上讲,
    .map
    是每个
    的超集,但是从实现角度来看,如果您实际上不需要结果,那么使用它是不明智的。

    \uz每个都不会返回值 _.map返回一个值

    例如:

    var animals = [ "dog","cat","pig" ]
    
    var newAnimalEach = _.each(animals,(animal)=>{ return animal+'s'})
    console.log(newAnimalEach) // returns [ "dog","cat","pig" ]
    
    var newAnimalMap = _.map(animals,(animal)=>{ return animal+'s'})
    console.log(newAnimalMap) // returns [ "dogs","cats","pigs" ]
    

    你读过关于这些函数的文档吗?我是JavaScript新手,我试图阅读下划线文档,但我不理解其中的一些术语。