Files
lodash/src/map.ts
2023-09-16 14:47:50 -07:00

31 lines
758 B
TypeScript

/**
* Creates an array of values by running each element of `array` thru `iteratee`.
* The iteratee is invoked with three arguments: (value, index, array).
*
* @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
*
* function square(n) {
* return n * n
* }
*
* map([4, 8], square)
* // => [16, 64]
*/
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;