none
What is the reactive/functional way to iterate over a dictionary? RRS feed

  • Question

  • I could not find an information on how to iterate over a dictionary. 

    Normally I would iterate as so:

    for (var key in dictionary) {
        console.log("key: " + key ", value: " + dictionary[key]);
    }


    Currently I have a function

    function fromDictionary(dictionary) {
        return rx.Observable.fromArray(Object.keys(dictionary))
          .map(function(key){			
            return {key:key, value:dictionary[key]};		
          });
    }

    then used as so:

    fromDictionary(dictionary)
        .subscribe(function(keyValue){
            console.log("key: " + key + ", value: " + value);
        });

    Is there a better way to iterate over a dictionary? 


    • Edited by Ray Lim Wednesday, December 10, 2014 7:12 AM
    Wednesday, December 10, 2014 7:08 AM

All replies

  • 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.

    Why not just use native JS iteration techniques like a for loop?

    - Dave


    http://davesexton.com/blog

    • Proposed as answer by LeeCampbell Tuesday, April 28, 2015 10:56 AM
    Thursday, December 11, 2014 4:13 AM