Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/modules / ___dist / chain-able / src / deps / loop / map / mapArrayKeys.js
Size: Mime:
const ObjectKeys = require('../../util/keys')
const toArray = require('../../cast/toArray')
const preAllocate = require('../../array/preAllocate')

/**
 * 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} array The array to iterate over.
 * @param {Function} iteratee The function invoked per iteration.
 * @return {Array} 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(array, iteratee) {
  // const arr = toArray(array)
  const result = preAllocate(array)

  for (let i = 0; i < array.length; i++) {
    const key = array[i]
    const value = array[key]
    result[iteratee(value, key, array)] = value
  }

  return result
}

module.exports = mapIndex