mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-06 01:47:48 +00:00
Split map out.
This commit is contained in:
38
map.js
38
map.js
@@ -1,23 +1,10 @@
|
||||
import arrayMap from './.internal/arrayMap.js'
|
||||
import baseMap from './.internal/baseMap.js'
|
||||
|
||||
/**
|
||||
* Creates an array of values by running each element in `collection` thru
|
||||
* `iteratee`. The iteratee is invoked with three arguments:
|
||||
* (value, index|key, collection).
|
||||
* Creates an array of values by running each element of `array` thru `iteratee`.
|
||||
* The iteratee is invoked with three arguments: (value, index, array).
|
||||
*
|
||||
* Many lodash methods are guarded to work as iteratees for methods like
|
||||
* `every`, `filter`, `map`, `mapValues`, `reject`, and `some`.
|
||||
*
|
||||
* The guarded methods are:
|
||||
* `ary`, `chunk`, `curry`, `curryRight`, `drop`, `dropRight`, `every`,
|
||||
* `fill`, `invert`, `parseInt`, `random`, `range`, `rangeRight`, `repeat`,
|
||||
* `sampleSize`, `slice`, `some`, `sortBy`, `split`, `take`, `takeRight`,
|
||||
* `template`, `trim`, `trimEnd`, `trimStart`, and `words`
|
||||
*
|
||||
* @since 0.1.0
|
||||
* @category Collection
|
||||
* @param {Array|Object} collection The collection to iterate over.
|
||||
* @since 5.0.0
|
||||
* @category Array
|
||||
* @param {Array} array The array to iterate over.
|
||||
* @param {Function} iteratee The function invoked per iteration.
|
||||
* @returns {Array} Returns the new mapped array.
|
||||
* @example
|
||||
@@ -28,13 +15,16 @@ import baseMap from './.internal/baseMap.js'
|
||||
*
|
||||
* map([4, 8], square)
|
||||
* // => [16, 64]
|
||||
*
|
||||
* map({ 'a': 4, 'b': 8 }, square)
|
||||
* // => [16, 64] (iteration order is not guaranteed)
|
||||
*/
|
||||
function map(collection, iteratee) {
|
||||
const func = Array.isArray(collection) ? arrayMap : baseMap
|
||||
return func(collection, iteratee)
|
||||
function map(array, iteratee) {
|
||||
let index = -1
|
||||
const length = array == null ? 0 : array.length
|
||||
const result = new Array(length)
|
||||
|
||||
while (++index < length) {
|
||||
result[index] = iteratee(array[index], index, array)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
export default map
|
||||
|
||||
Reference in New Issue
Block a user