How to iterate over a JavaScript object?

后端 未结 18 1687
失恋的感觉
失恋的感觉 2020-11-21 22:52

I have an object in JavaScript:

{
    abc: \'...\',
    bca: \'...\',
    zzz: \'...\',
    xxx: \'...\',
    ccc: \'...\',
    // ...
}

I

18条回答
  •  旧巷少年郎
    2020-11-21 23:07

    var Dictionary = {
      If: {
        you: {
          can: '',
          make: ''
        },
        sense: ''
      },
      of: {
        the: {
          sentence: {
            it: '',
            worked: ''
          }
        }
      }
    };
    
    function Iterate(obj) {
      for (prop in obj) {
        if (obj.hasOwnProperty(prop) && isNaN(prop)) {
          console.log(prop + ': ' + obj[prop]);
          Iterate(obj[prop]);
        }
      }
    }
    Iterate(Dictionary);
    

提交回复
热议问题