How to iterate (keys, values) in javascript?

前端 未结 10 2691
孤街浪徒
孤街浪徒 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 10:08

    WELCOME TO 2020 *Drools in ES6*

    Theres some pretty old answers in here - take advantage of destructuring. In my opinion this is without a doubt the nicest (very readable) way to iterate an object.

    Object.entries(myObject).forEach(([k,v]) => {
        console.log("The key: ",k)
        console.log("The value: ",v)
    })
    
    0 讨论(0)
  • 2020-11-27 10:08

    I think the fast and easy way is

    Object.entries(event).forEach(k => {
        console.log("properties ... ", k[0], k[1]); });
    

    just check the documentation https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

    0 讨论(0)
  • 2020-11-27 10:08

    You can use below script.

    var obj={1:"a",2:"b",c:"3"};
    for (var x=Object.keys(obj),i=0;i<x.length,key=x[i],value=obj[key];i++){
        console.log(key,value);
    }
    

    outputs
    1 a
    2 b
    c 3

    0 讨论(0)
  • 2020-11-27 10:09

    Try this:

    var value;
    for (var key in dictionary) {
        value = dictionary[key];
        // your code here...
    }
    
    0 讨论(0)
提交回复
热议问题