The arrMap() method creates a new array populated with the results of calling a provided function on every element in the calling array.

arrMap calls a provided callbackFn function once for each element in an array, in order, and constructs a new array from the results. callbackFn is invoked only for indexes of the array which have assigned values (including undefined).

It is not called for missing elements of the array; that is:

  • indexes that have never been set;
  • indexes which have been deleted.

0.3.3

const numbers = [1, 4, 9];
const roots = arrMap(numbers, (num) => Math.sqrt(num));

// roots is now [1, 2, 3]
// numbers is still [1, 4, 9]

const kvArray = [{ key: 1, value: 10 },
{ key: 2, value: 20 },
{ key: 3, value: 30 }];

const reformattedArray = arrMap(kvArray, ({ key, value}) => ({ [key]: value }));

// reformattedArray is now [{1: 10}, {2: 20}, {3: 30}],

// kvArray is still:
// [{key: 1, value: 10},
// {key: 2, value: 20},
// {key: 3, value: 30}]

// Also supports Array Like objects with same output
const kvArray = {
length: 3,
0: { key: 1, value: 10 },
1: { key: 2, value: 20 },
2: { key: 3, value: 30 }
};
  • Type Parameters

    • T

      Identifies the type of the array elements

    • R = T

      Identifies the type of the elements returned by the callback function, defaults to T.

    Parameters

    • theArray: ArrayLike<T>

      The array or array like object of elements to be searched.

    • callbackFn: ArrMapCallbackFn<T, R>

      The function that is called for evetn element of theArray.

    • OptionalthisArg: any

      The value to use as the this when executing the callbackFn.

    Returns R[]