如何在reactJS中映射字典? [英] How to map a dictionary in reactJS?

查看:308
本文介绍了如何在reactJS中映射字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以{{key:object},{key:object},...形式从在线api获取字典。对于类似1000对象}。我想使用reactJS做类似的事情

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?

推荐答案

Javascript中的字典被称为对象,您可以在非常相似的情况下迭代它们数组方式。

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

如果你在考虑使用map,以便在迭代结束时你有一个完整的数组,然后在你的 for..in 循环中你可以推送到你之前声明的数组。

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

这篇关于如何在reactJS中映射字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆