Last active
December 12, 2017 22:12
-
-
Save avastamin/34eb566ebc9f890330a247d2c51c0c83 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Programming Quiz: Make An Iterable Object | |
* | |
* Turn the `james` object into an iterable object. | |
* | |
* Each call to iterator.next should log out an object with the following info: | |
* - key: the key from the `james` object | |
* - value: the value of the key from the `james` object | |
* - done: true or false if there are more keys/values | |
* | |
* For clarification, look at the example console.logs at the bottom of the code. | |
*/ | |
let james = { | |
name: 'James', | |
height: `5'10"`, | |
weight: 185 | |
}; | |
function makeObjectIterable(object) { | |
if (!object[Symbol.iterator]) { | |
object[Symbol.iterator] = function* () { | |
const properties = Object.keys(object); | |
for (let prop of properties) { | |
yield this[prop]; | |
} | |
}; | |
} | |
return object; | |
}; | |
let iterable = makeObjectIterable(james) | |
let iterator = iterable[Symbol.iterator](); | |
console.log(iterator.next().value); // 'James' | |
console.log(iterator.next().value); // `5'10` | |
console.log(iterator.next().value); // 185 | |
//......................................................// | |
/* | |
* Programming Quiz: Make An Iterable Object | |
* | |
* Turn the `james` object into an iterable object. | |
* | |
* Each call to iterator.next should log out an object with the following info: | |
* - key: the key from the `james` object | |
* - value: the value of the key from the `james` object | |
* - done: true or false if there are more keys/values | |
* | |
* For clarification, look at the example console.logs at the bottom of the code. | |
* | |
* Hints: | |
* - Use `Object.keys()` to store the object's properties in an array. | |
* - Each call to `iterator.next()` should use this array to know which property to return. | |
* - You can access the original object using `this`. | |
* - To access the values of the original object, use `this` and the key from the `Object.keys()` array. | |
*/ | |
const james = { | |
name: 'James', | |
height: `5'10"`, | |
weight: 185, | |
[Symbol.iterator]: function () { | |
var self = this; | |
var values = Object.keys(this); | |
var i = 0; | |
var j = 0; | |
var isDone = false; | |
return { | |
next: function () { | |
if(i >= values.length-1){ | |
isDone = true; | |
} | |
return { | |
value: self[values[i++]], | |
key: values[j++], | |
done: isDone | |
} | |
} | |
} | |
} | |
}; | |
// const iterator = james[Symbol.iterator](); | |
// | |
// console.log(iterator.next().value); // 'James' | |
// console.log(iterator.next().value); // `5'10` | |
// console.log(iterator.next().value); // 185 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment