How to map a dictionary in reactJS?

前端 未结 4 555
暖寄归人
暖寄归人 2020-12-31 04:08

I am getting a dictionary from an online api in the form of {{key: object}, {key: object},... For like 1000 Objects}. I would like to use reactJS to do something like

4条回答
  •  旧时难觅i
    2020-12-31 05:13

    "Dictionaries" in Javascript are called objects and you can iterate over them in a very similar way to arrays.

    var dict = this.props.dict;
    
    for (var key in dict) {
      // Do stuff. ex: console.log(dict[key])
    }
    

    If you were thinking of using map so that at the end of the iteration you had a complete array, then inside your for..in loop you could push to an array you declare earlier.

    var dict = this.props.dict;
    var arr = [];
    
    for (var key in dict) {
      arr.push(dict[key]);
    }
    

提交回复
热议问题