JSFiddle - React, Tailwind, and code Playground
by ruhul105
JavaScript
/*
* 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