How to iterate (keys, values) in javascript?

前端 未结 10 2721
孤街浪徒
孤街浪徒 2020-11-27 09:23

I have a dictionary that has the format of

dictionary = {0: {object}, 1:{object}, 2:{object}}

How can I iterate through this dictionary by

10条回答
  •  醉话见心
    2020-11-27 09:57

    As an improvement to the accepted answer, in order to reduce nesting, you could do this instead, provided that the key is not inherited:

    for (var key in dictionary) {
        if (!dictionary.hasOwnProperty(key)) {
            continue;
        }
        console.log(key, dictionary[key]);
    }
    

    Edit: info about Object.hasOwnProperty here

提交回复
热议问题