47

I am trying to loop over a JavaScript object in ES6.

 for (let [value, index] of object) {
    do something with rest
    if (index >= 1) {
       // do something with first item
    }
  }

It works fine, although when I try to use index to get the first item it returns an error in console:

Uncaught TypeError: Invalid attempt to destructure non-iterable instance

Any ideas on how to loop over an object with index? thanks

4
  • 5
    Do you realize that properties have no specific order so an index really doesn't mean anything for properties of an object? I suspect that this is just the wrong way to do whatever you're trying to do. You can use Object.keys() to get an array of property names and then iterate that and there will be an index then, though again, it's probably not meaningful. Commented Jul 22, 2017 at 6:58
  • Thanks for the insight. I will try and change the object to an array which sounds more logical if I need to get first item. Commented Jul 22, 2017 at 7:08
  • Yes, an array makes more sense and then your for/of loop will work as you want. Commented Jul 22, 2017 at 7:14
  • What is your object? Please post the whole code that throws this error (it suggest that your object is iterable, but the elements are not). Commented Jul 22, 2017 at 13:21

5 Answers 5

69

This is just meant to be an addition to jonas w's solutions.

If you need the key of the current value:

const object = {a:2, b:4, c:6, d:8};

for (const [index, [key, value]] of Object.entries(Object.entries(object))) {
  console.log(`${index}: ${key} = ${value}`);
}

Object.entries(object).forEach(([key, value], index) => {
  console.log(`${index}: ${key} = ${value}`);
});

Of course, you can leave out the key at any time:

const object = {a:2, b:4, c:6, d:8};

for (const [index, [, value]] of Object.entries(Object.entries(object))) {
  console.log(`${index}: ${value}`);
}

Object.entries(object).forEach(([, value], index) => {
  console.log(`${index}: ${value}`);
});

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

1 Comment

Simple hack to do it.
33

Simply count the index:

let index = 0;
for (let value of object) {
  //do something with rest
  if (index >= 1) {
    // do something with the third and following items
  }
  index++;
}

Or if you really want to use object destructuring ( i dont know why ) its a bit more complicated:

let entries = Object.entries(object);

for(let [index, [key, value]] of entries.entries()){
 //...
}

or:

for(let [index,value] of Object.values(object).entries()){
  //...
}

But i dont know why youre not using a simple forEach?:

Object.values(obj).forEach((value, index)=> /*...*/);

3 Comments

Hi Jonas, thanks for your answer. But I was hoping there was a cleaner and neater way to do this in ES6.
@londonfed shure there are a few approaches.
Ok a simple forEach sounds like best way to go. Thanks again
12

index, key:

for (const [index, key] of Object.keys(object).entries()) {
  // ...
}

index, value:

for (const [index, value] of Object.values(object).entries()) {
  // ...
}

index, key, value:

for (const [index, [key, value]] of Object.entries(object).entries()) {
  // ...
}

Comments

2

Just count the index:

let index = 0;
for (let value of object) {
  if (index === 1) {
    // do something with idx 1
  }
  // do something with the rest...
  index++;
}

Comments

1

Just use:

const obj = {
  name: 'Bob',
  age: 23,
  city: 'Mexico',
};

for (const [key, value] of Object.entries(obj)) {
  console.log(key, '=>', 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.