Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
29
rated 0 times [  31] [ 2]  / answers: 1 / hits: 114040  / 8 Years ago, thu, february 4, 2016, 12:00:00

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



this.props.dict.map(function(object, key)){
//Do stuff
}


This map works with arrays but it obviously doesn't work with dictionaries. How can I achieve something similar?


More From » reactjs

 Answers
6

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]);
}

[#63451] Tuesday, February 2, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hasanb

Total Points: 321
Total Questions: 102
Total Answers: 96

Location: Burkina Faso
Member since Fri, Sep 4, 2020
4 Years ago
;