Groovy .collect() 相当于 JS .map() 吗?

Ami*_*.io 6 javascript groovy

Groovy .collect() 相当于 JS .map() 吗?还是另有目的?

Str*_*lok 5

它也可以做一些其他的事情(比如初始收集),但除此之外它几乎是一样的:

\n\n
// Collect without\n// initial collection.\nassert [1,2,8] == [1,4,64].collect(Math.&sqrt)\nassert [0,2,4,6] == (0..3).collect { it * 2 }\nassert [\'Groovy\', \'Grails\'] == [lang: \'Groovy\', framework: \'Grails\'].collect { it.value }\n\xc2\xa0\n// Collect with initial collection argument.\nassert [0, 1, 2, 3] == [2, 3].collect([0, 1]) { it }\nassert [0, 3, 6, 9] == [2, 3].collect([0, 3], { it * 3})\nassert [\'Gradle\', \'groovy\', \'grails\'] == [\'Groovy\', \'Grails\'].collect([\'Gradle\']) { it.toLowerCase() }\nassert [\'m\',\'r\',\'h\',\'a\',\'k\',\'i\'] == [4, -3, 7, 5].collect([\'m\', \'r\']) { (it + 100) as char }\n
Run Code Online (Sandbox Code Playgroud)\n\n

附言。示例归功于mrhaki出色的 Groovy Goodness 博客

\n