0

How do I access the properties of james and obtain the below output using console.log statements

console.log(iterator.next().value); 'James'
console.log(iterator.next().value); 5'10
console.log(iterator.next().value); 185

const james = {  
name: 'James',  
height: `5'10"`,  
weight: 185};  
const key=Object.keys(james);  
const iterator = james[[Symbol.iterator]](); 

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

I'm new to JavaScript and programming..
Thanks in advance

4

3 Answers 3

1

Add the following iterator method to the object:

const james = {
  name: 'James',
  height: `5'10"`,
  weight: 185,
  [Symbol.iterator]() {
    let nextIndex = 0;
    const entries = Object.entries(this);
      return {
       next: () => {
        if(nextIndex >= entries.length) {
          return {done: true};
        }

        const [key, value] = entries[nextIndex++];

        return {value: { key, value }, done: false};
      }
    };
  }
};

const iterator = james[Symbol.iterator]();

console.log(iterator.next().value);
console.log(iterator.next().value);
console.log(iterator.next().value);

console.log('----------------');

// or iterate with for...of

for(const vk of james) {
  console.log(vk);
}

Or as @Bergi suggested, instead of manually creating the iterator method, you can map the entries to an array of objects, and return the array's iterator:

const james = {
  name: 'James',
  height: `5'10"`,
  weight: 185,
  [Symbol.iterator]() {
    return Object.entries(this).map(([key, value]) => ({key, value}))[Symbol.iterator]();
  }
};

const iterator = james[Symbol.iterator]();

console.log(iterator.next().value);
console.log(iterator.next().value);
console.log(iterator.next().value);

console.log('----------------');

// or iterate with for...of

for(const vk of james) {
  console.log(vk);
}

Sign up to request clarification or add additional context in comments.

1 Comment

Object.entries(this).map(([key, value]) => ({key, value}))[Symbol.iterator]() would've been simpler :-)
0

Just use the ES8 Object.values instead of Object.keys in your code to get the values:

const iterator = Object.values({  
    name: 'James',  
    height: `5'10"`,  
    weight: 185
})[Symbol.iterator]();
console.log(iterator.next().value); // James
console.log(iterator.next().value); // 5'10"
console.log(iterator.next().value); // 185

Comments

0

You've already got some good suggestions here, but I figured I'll add one more that can be nice if you can use generators:

const james = {
  name: 'James',
  height: `5'10"`,
  weight: 185,
  *[Symbol.iterator]() {
    for (const [key, value] of Object.entries(this)) {
      yield { key, value };
    }
  },
};

of course given

const james = {  
  name: 'James',  
  height: `5'10"`,  
  weight: 185,
};  
const key = Object.keys(james);  
const iterator = james[[Symbol.iterator]]();

console.log(iterator.next().value); // 'James'
console.log(iterator.next().value); // 5'10
console.log(iterator.next().value); // 185

you could also avoid the custom iterator and do

const james = {  
  name: 'James',  
  height: `5'10"`,  
  weight: 185,
};  
const iterator = Object.entries(james)[Symbol.iterator]();

console.log(iterator.next()[1]); // 'James'
console.log(iterator.next()[1]); // 5'10
console.log(iterator.next()[1]); // 185

or even

const james = {  
  name: 'James',  
  height: `5'10"`,  
  weight: 185,
};  
for (const value of Object.values(james)) {
  console.log(value);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.