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
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.for/ofloop will work as you want.object? Please post the whole code that throws this error (it suggest that yourobjectis iterable, but the elements are not).