mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-04 00:57:48 +00:00
Split map out.
This commit is contained in:
29
mapObject.js
Normal file
29
mapObject.js
Normal file
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Creates an array of values by running each property of `object` thru
|
||||
* `iteratee`. The iteratee is invoked with three arguments: (value, key, object).
|
||||
*
|
||||
* @since 5.0.0
|
||||
* @category Object
|
||||
* @param {Object} object The object 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({ 'a': 4, 'b': 8 }, square)
|
||||
* // => [16, 64] (iteration order is not guaranteed)
|
||||
*/
|
||||
function mapObject(object, iteratee) {
|
||||
const props = Object.keys(object);
|
||||
const result = new Array(props.length)
|
||||
|
||||
props.forEach((key, index) => {
|
||||
result[index] = iteratee(object[key], key, object)
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
export default mapObject
|
||||
Reference in New Issue
Block a user