Repository navigation
Expand file tree
/
Copy pathobjects.js
More file actions
63 lines (55 loc) · 1.35 KB
/
Copy pathobjects.js
File metadata and controls
63 lines (55 loc) · 1.35 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Complete the following underscore functions.
// Reference http://underscorejs.org/ for examples.
const keys = (obj) => {
return Object.keys(obj);
};
const values = (obj) => {
return Object.values(obj);
};
const mapObject = (obj, cb) => {
const result = {};
const objKeys = Object.keys(obj);
const objValues = Object.values(obj);
for (let i = 0; i < objValues.length; i++) {
result[objKeys[i]] = cb(objValues[i]);
}
return result;
};
const pairs = (obj) => {
const pairsArr = [];
const objkeys = Object.keys(obj);
const objValues = Object.values(obj);
for (let i = 0; i < objkeys.length; i++) {
pairsArr.push([objkeys[i], objValues[i]]);
}
return pairsArr;
};
/* STRETCH PROBLEMS */
const invert = (obj) => {
const result = {};
const objKeys = Object.keys(obj);
const objValues = Object.values(obj);
for (let i = 0; i < objKeys.length; i++) {
result[objValues[i]] = objKeys[i];
}
return result;
};
const defaults = (obj, defaultProps) => {
const defaultKeys = Object.keys(defaultProps);
const defaultValues = Object.values(defaultProps);
for (let i = 0; i < defaultKeys.length; i++) {
if (!(defaultKeys[i] in obj)) {
obj[defaultKeys[i]] = defaultValues[i];
}
}
return obj;
};
/* eslint-enable no-unused-vars */
module.exports = {
keys,
values,
mapObject,
pairs,
invert,
defaults,
};