[Javascript] Iterate Over Items with JavaScript's for-of Loop

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Javascript] Iterate Over Items with JavaScript's for-of Loop相关的知识,希望对你有一定的参考价值。

In this lesson we will understand the For Of loop in javascript which was introduced in ES6. The for-of loop lets you iterate of an itterable object (array, string, set, or map) and returns each objects value in a specified variable. This excludes plain objects as we will see in the lesson.

 

Object.prototype.objCustom = function() {}; 
Array.prototype.arrCustom = function() {};

let iterable = [3, 5, 7];
iterable.foo = ‘hello‘;

for (let i in iterable) {
  console.log(i); // 0, 1, 2, "foo", "arrCustom", "objCustom"
}

for (let i in iterable) {
  if (iterable.hasOwnProperty(i)) {
    console.log(i); // 0, 1, 2, "foo"
  }
}

for (let i of iterable) {
  console.log(i); // 3, 5, 7
}

 

"accCustom" and "objCustom" are not logged in second loop is because they are inherited. Not array and object‘s own prop.

 

以上是关于[Javascript] Iterate Over Items with JavaScript's for-of Loop的主要内容,如果未能解决你的问题,请参考以下文章

How to Iterate Over a Map in Java?

[ES2017] Iterate over properties of an object with ES2017 Object.entries()

python 来自http://stackoverflow.com/questions/12325608/iterate-over-a-dict-or-list-in-python/12325691#

How do I iterate over a Scala List (or more generally, a sequence) using theforeach method or for lo

如何迭代pandas dataframe的行

javascript 发送请求over tor并续订ip