Для тих , хто використовує об'єкти , які не є Arrayабо навіть масив , як ви можете створити свою власну ітерацію легко , так що ви все ще можете використовувати for ofдля таких речей , як , localStorageякі на насправді є тільки length:
function indexerator(length) {
var output = new Object();
var index = 0;
output[Symbol.iterator] = function() {
return {next:function() {
return (index < length) ? {value:index++} : {done:true};
}};
};
return output;
}
Тоді просто нагодуйте це число:
for (let index of indexerator(localStorage.length))
console.log(localStorage.key(index))
for-ofз ,.entries()і це в два рази повільніше за порівнянні з.forEach(). jsperf.com/for-of-vs-foreach-with-index