Using Objects in For Of Loops

后端 未结 14 2100
春和景丽
春和景丽 2020-11-28 05:56

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:

te         


        
14条回答
  •  孤街浪徒
    2020-11-28 06:07

    What about using

    function* entries(obj) {
        for (let key of Object.keys(obj)) {
            yield [key, obj[key]];
        }
    }
    
    for ([key, value] of entries({a: "1", b: "2"})) {
        console.log(key + " " + value);
    }
    

提交回复
热议问题