Using Objects in For Of Loops

后端 未结 14 2082
春和景丽
春和景丽 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:04

    Using Array Destruction you can iterate it as follows using forEach

    const obj = { a: 5, b: 7, c: 9 };
    
    Object.entries(obj).forEach(([key, value]) => {
      console.log(`${key} ${value}`); // "a 5", "b 7", "c 9"
    });
    

提交回复
热议问题