Repository navigation
Expand file tree
/
Copy pathobjects.js
More file actions
87 lines (80 loc) · 2.45 KB
/
Copy pathobjects.js
File metadata and controls
87 lines (80 loc) · 2.45 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
// Complete the following underscore functions.
// Reference http://underscorejs.org/ for examples.
const keys = (obj) => {
return Object.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
};
const names = { name: 'time', place: 'weather' };
// console.log(keys(names));
const values = (obj) => {
return Object.keys(obj).map((value) => {
return value = obj[value];
});
// Return all of the values of the object's own properties.
// Ignore functions
// http://underscorejs.org/#values
};
// console.log(values(names));
const mapObject = (obj, cb) => {
// Like map for arrays, but for objects. Transform the value of each property in turn.
// http://underscorejs.org/#mapObject
const keyArr = Object.keys(obj);
const valueArr = Object.keys(obj).map((value) => {
return value = cb(obj[value]);
});
for (let i = 0; i < valueArr.length; i++) obj[keyArr[i]] = valueArr[i];
return obj;
};
// console.log(mapObject(names, values));
const pairs = (obj) => {
// Convert an object into a list of [key, value] pairs.
// http://underscorejs.org/#pairs
const arr1 = Object.keys(obj).map((value) => {
return value = obj[value];
});
const arr3 = [];
for (let i = 0; i < arr1.length; i++) {
const arr4 = [];
arr4.push(Object.keys(obj)[i]);
arr4.push(arr1[i]);
arr3.push(arr4);
}
return arr3;
};
const homes = { name: 'victorian', place: 'weather' };
// console.log(pairs(homes));
const invert = (obj) => {
const obj2 = {};
Object.keys(obj).map((value) => {
return obj2[obj[value]] = value;
});
// console.log(obj2);
return obj2;
// 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
};
const friends = { name: 'tim' };
// console.log(invert(friends));
const filled = { name: 'south' };
const defaults = (obj, defaultProps) => {
// Fill in undefined properties that match properties on the `defaultProps` parameter object.
// Return `obj`.
// http://underscorejs.org/#defaults
Object.keys(defaultProps).forEach((x) => {
if (obj[x] === undefined) obj[x] = defaultProps[x];
});
return obj;
};
// console.log(defaults(filled, friends));
/* eslint-enable no-unused-vars */
module.exports = {
keys,
values,
mapObject,
pairs,
invert,
defaults
};