Repository URL to install this package:
|
Version:
6.0.4 ▾
|
import preAllocate from '../../array/preAllocate'
import { MapIndexIteratee } from './typings.loop.map'
/**
* The opposite of `mapValue` this method creates an ARRAY with the
* same values as `ARRAY` and keys generated by running each own enumerable
* string keyed property of `ARRAY` thru `iteratee`. The iteratee is invoked
* with three arguments: (value, key, ARRAY).
* @since 5.0.0-beta.6
* @memberOf loop
* @alias mapArrayIndex
* @alias mapArrayIndexes
* @alias mapIndex
*
* @param array The array to iterate over.
* @param iteratee The function invoked per iteration.
* @return Returns the new mapped ARRAY.
*
* @fork 3.8.0
* @category Array
*
* @see mapValue
*
* @example
*
* mapKey({ 'a': 1, 'b': 2 }, (value, key) => key + value)
* //=> { 'a1': 1, 'b2': 2 }
*
*/
function mapIndex<Obj extends { [Symbol.iterator]: () => Iterator<any> }>(
/**
* is actually the obj
*/
array: Obj | any[],
iteratee: MapIndexIteratee<Obj>
) {
// const arr = toArray(array)
const result = preAllocate(array)
for (const key of array) {
const value = array[key]
result[iteratee(value, key, array)] = value
}
return result
}
export default mapIndex