Using Objects in For Of Loops
问题 Why isn't is possible to use objects in for of loops? Or is this a browser bug? This code doesn't work in Chrome 42, saying undefined is not a function: test = { first: "one"} for(var item of test) { console.log(item) } 回答1: The for..of loop only supports iterable objects like arrays, not objects. To iterate over the values of an object, use: for (var key in test) { var item = test[key]; } 回答2: You can use this syntax: let myObject = {first: "one"}; for(let [key, value] of Object.entries