-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathobjects.js
More file actions
89 lines (80 loc) · 2.57 KB
/
objects.js
File metadata and controls
89 lines (80 loc) · 2.57 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Complete the following underscore functions.
// Reference http://underscorejs.org/ for examples.
const keys = (obj) => {
// Retrieve all the names of the object's properties.
// Return the keys as strings in an array.
// Based on http://underscorejs.org/#keys
// output: an array containing the properties of obj
return Object.keys(obj);
};
const values = (obj) => {
// Return all of the values of the object's own properties.
// Ignore functions
// http://underscorejs.org/#values
// output: an array containing the values of obj
return Object.values(obj);
};
const mapObject = (obj, cb) => {
// Like map for arrays, but for objects. Transform the value of each property in turn.
// http://underscorejs.org/#mapObject
const props = Object.keys(obj);
const result = {};
for (let i = 0; i < props.length; i++) {
// pass all properties in obj into cb
result[props[i]] = cb(obj[props[i]]);
}
return result;
};
const pairs = (obj) => {
// Convert an object into a list of [key, value] pairs.
// http://underscorejs.org/#pairs
// output: list of key : value pairs in obj
return Object.entries(obj);
};
/* STRETCH PROBLEMS */
const invert = (obj) => {
// Returns a copy of the object where the keys have become the values and the values the keys.
// Assume that all of the object's values will be unique and string serializable.
// http://underscorejs.org/#invert
// method #1: use a loop
// let result = {}; // holds the inverted key value pairs
// let keys = Object.keys(obj);
// let vals = Object.values(obj);
// // iterate over obj's keys
// for (let key of keys) {
// // set props in result to be values of obj
// // values of result will be the properties of obj
// result[obj[key]] = key;
// }
// return result;
// method #2: use array.reduce()
return Object.keys(obj).reduce((a, b) => {
// initial value of a is {}
// set each of obj's keys to a property in a
a[obj[b]] = b;
// a accumulates inverted key:value pairs
return a;
}, {});
// output: an object with obj's keys as values and obj's values as properties
};
const defaults = (obj, defaultProps) => {
// Fill in undefined properties that match properties on the `defaultProps` parameter object.
// Return `obj`.
// http://underscorejs.org/#defaults
const props = Object.keys(defaultProps);
props.forEach((prop) => {
if (obj[prop] === undefined) {
obj[prop] = defaultProps[prop];
}
});
return obj;
};
/* eslint-enable no-unused-vars */
module.exports = {
keys,
values,
mapObject,
pairs,
invert,
defaults,
};