Skip to content

Commit 40506cf

Browse files
committed
Project W1(B)
-Arrays: Completed -Class: Completed -Closure: Completed -ES6: Completed -Objects: Completed
1 parent 8227a57 commit 40506cf

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
},
2828
"dependencies": {
2929
"babel-preset-es2015": "^6.24.0",
30-
"eslint-config-airbnb": "^14.1.0"
30+
"eslint-config-airbnb": "^14.1.0",
31+
"underscore": "^1.8.3"
3132
}
3233
}

src/objects.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,57 @@
11
// Complete the following underscore functions.
22
// Reference http://underscorejs.org/ for examples.
33

4-
const keys = (obj) => {
4+
const keys = obj => Object.keys(obj);
55
// Retrieve all the names of the object's properties.
66
// Return the keys as strings in an array.
77
// Based on http://underscorejs.org/#keys
8-
};
98

10-
const values = (obj) => {
9+
const values = obj => Object.values(obj);
1110
// Return all of the values of the object's own properties.
1211
// Ignore functions
1312
// http://underscorejs.org/#values
14-
};
1513

1614
const mapObject = (obj, cb) => {
1715
// Like map for arrays, but for objects. Transform the value of each property in turn.
1816
// http://underscorejs.org/#mapObject
17+
for (let i = 0; i < keys(obj).length; i++) {
18+
obj[keys(obj)[i]] = cb(values(obj)[i]);
19+
}
20+
return obj;
1921
};
20-
2122
const pairs = (obj) => {
2223
// Convert an object into a list of [key, value] pairs.
2324
// http://underscorejs.org/#pairs
25+
const outArr = [];
26+
for (let i = 0; i < keys(obj).length; i++) {
27+
const subArr = [];
28+
subArr.push(keys(obj)[i], values(obj)[i]);
29+
outArr[i] = subArr;
30+
}
31+
return outArr;
2432
};
2533

2634
const invert = (obj) => {
2735
// Returns a copy of the object where the keys have become the values and the values the keys.
2836
// Assume that all of the object's values will be unique and string serializable.
2937
// http://underscorejs.org/#invert
38+
const newObj = {};
39+
for (let i = 0; i < keys(obj).length; i++) {
40+
newObj[values(obj)[i]] = keys(obj)[i];
41+
}
42+
return newObj;
3043
};
3144

3245
const defaults = (obj, defaultProps) => {
3346
// Fill in undefined properties that match properties on the `defaultProps` parameter object.
3447
// Return `obj`.
3548
// http://underscorejs.org/#defaults
49+
for (let i = 0; i < keys(defaultProps).length; i++) {
50+
if (obj[keys(defaultProps)[i]] === undefined) {
51+
obj[keys(defaultProps)[i]] = values(defaultProps)[i];
52+
}
53+
}
54+
return obj;
3655
};
3756

3857
/* eslint-enable no-unused-vars */

0 commit comments

Comments
 (0)