迭代字典的反应/功能方式是什么? [英] What is the reactive/functional way to iterate over a dictionary?
本文介绍了迭代字典的反应/功能方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我找不到有关如何迭代字典的信息。
I could not find an information on how to iterate over a dictionary.
通常情况下我会迭代:
for (var key in dictionary) {
console.log("key: " + key ", value: " + dictionary[key]);
}
目前我有一个功能
function fromDictionary(dictionary) {
return rx.Observable.fromArray(Object.keys(dictionary))
.map(function(key){
return {key:key, value:dictionary[key]};
});
}
然后用作:
fromDictionary(dictionary)
.subscribe(function(keyValue){
console.log("key: " + key + ", value: " + value);
});
有没有更好的方法来迭代字典?
Is there a better way to iterate over a dictionary?
推荐答案
这样做的目的是什么? 字典不是异步数据结构,因此将它们转换为"反应"数据结构。这样的模型只会使它们更难以使用。
What is the purpose of doing this? Dictionaries aren't asynchronous data structures, so converting them into a "reactive" model in this way is just making them more difficult to work with.
为什么不使用本机JS迭代技术,如 for 循环?
Why not just use native JS iteration techniques like a for loop?
- 戴夫
这篇关于迭代字典的反应/功能方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文