-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapObject.js
More file actions
36 lines (21 loc) · 786 Bytes
/
mapObject.js
File metadata and controls
36 lines (21 loc) · 786 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
Like map for arrays, but for objects.
Transform the value of each property in turn by passing it to the callback function.
http://underscorejs.org/#mapObject
*/
const keys=require('./keys');
function mapObject(obj, cb) {
let newObj={};
/*if we want to pass index in cb so for this we generate keysArr and uses index
of each key bcz object does'nt provide any index
*/
let keysArr=keys(obj);
for(let index=0;index<keysArr.length;index++){
let currentValue=(obj[keysArr[index]]);
let transformedValue=cb(currentValue,index);
//here we are storing transformedValue in same key of newObj
newObj[keysArr[index]]=transformedValue;
}
return newObj;
}
module.exports=mapObject;