Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81158 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule mapObject
10
*/
11
12
'use strict';
13
14
var hasOwnProperty = Object.prototype.hasOwnProperty;
15
16
/**
17
* Executes the provided `callback` once for each enumerable own property in the
18
* object and constructs a new object from the results. The `callback` is
19
* invoked with three arguments:
20
*
21
* - the property value
22
* - the property name
23
* - the object being traversed
24
*
25
* Properties that are added after the call to `mapObject` will not be visited
26
* by `callback`. If the values of existing properties are changed, the value
27
* passed to `callback` will be the value at the time `mapObject` visits them.
28
* Properties that are deleted before being visited are not visited.
29
*
30
* @grep function objectMap()
31
* @grep function objMap()
32
*
33
* @param {?object} object
34
* @param {function} callback
35
* @param {*} context
36
* @return {?object}
37
*/
38
function mapObject(object, callback, context) {
39
if (!object) {
40
return null;
41
}
42
var result = {};
43
for (var name in object) {
44
if (hasOwnProperty.call(object, name)) {
45
result[name] = callback.call(context, object[name], name, object);
46
}
47
}
48
return result;
49
}
50
51
module.exports = mapObject;
52
53