Skip to content

Commit ddd493f

Browse files
committed
finished all of objects.js and passed all tests
1 parent e55ab27 commit ddd493f

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

‎src/objects.js‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,75 @@
22
// Reference http://underscorejs.org/ for examples.
33

44
const keys = (obj) => {
5+
return Object.getOwnPropertyNames(obj);
56
// Retrieve all the names of the object's properties.
67
// Return the keys as strings in an array.
78
// Based on http://underscorejs.org/#keys
89
};
910

1011
const values = (obj) => {
12+
const tvalues = [];
13+
const thekeys = keys(obj);
14+
for (let i = 0; i < thekeys.length; i++) {
15+
if (typeof (obj[thekeys[i]]) === 'function');
16+
else {
17+
tvalues.push(obj[thekeys[i]]);
18+
}
19+
}
20+
return tvalues;
1121
// Return all of the values of the object's own properties.
1222
// Ignore functions
1323
// http://underscorejs.org/#values
1424
};
1525

1626
const mapObject = (obj, cb) => {
27+
const mappedobj = {};
28+
const thekeys = keys(obj);
29+
const thevalues = values(obj);
30+
for (let i = 0; i < thekeys.length; i++) {
31+
mappedobj[thekeys[i]] = cb(thevalues[i]);
32+
}
33+
return mappedobj;
1734
// Like map for arrays, but for objects. Transform the value of each property in turn.
1835
// http://underscorejs.org/#mapObject
1936
};
2037

2138
const pairs = (obj) => {
39+
const thekeys = keys(obj);
40+
const thevalues = values(obj);
41+
const pairarray = [];
42+
for (let i = 0; i < thekeys.length; i++) {
43+
pairarray.push([thekeys[i], thevalues[i]]);
44+
}
45+
return pairarray;
2246
// Convert an object into a list of [key, value] pairs.
2347
// http://underscorejs.org/#pairs
2448
};
2549

2650
const invert = (obj) => {
51+
const newobj = {};
52+
const thekeys = keys(obj);
53+
const thevalues = values(obj);
54+
for (let i = 0; i < thekeys.length; i++) {
55+
newobj[thevalues[i]] = thekeys[i];
56+
}
57+
return newobj;
2758
// Returns a copy of the object where the keys have become the values and the values the keys.
2859
// Assume that all of the object's values will be unique and string serializable.
2960
// http://underscorejs.org/#invert
3061
};
3162

3263
const defaults = (obj, defaultProps) => {
64+
const defaultkeys = keys(defaultProps);
65+
const defaultvalues = values(defaultProps);
66+
const thekeys = keys(obj);
67+
for (let i = 0; i < defaultkeys.length; i++) {
68+
if (Object.prototype.hasOwnProperty.call(obj, defaultkeys[i]));
69+
else {
70+
obj[defaultkeys[i]] = defaultvalues[i];
71+
}
72+
}
73+
return obj;
3374
// Fill in undefined properties that match properties on the `defaultProps` parameter object.
3475
// Return `obj`.
3576
// http://underscorejs.org/#defaults

0 commit comments

Comments
 (0)