Archived

This forum has been archived. Please start a new discussion on GitHub.

iterate through javascript hash map in key order

Is there a way to iterate through a javascript Ice::HashMap in key order. Since this represents a dictionary, I would assume this would supported but both looping through the entries field or using forEach does not do it.

Comments

  • mes
    mes California
    Hi Dennis,

    You're correct, we don't currently provide a way to iterate over the keys in a sorted order. For now, I suggest that you collect the keys in an array, sort the array, and then iterate through the array entries to retrieve map values.

    ECMAScript 6 has a keys() method in their Map proposal. We'll add something similar to our type for the 3.6.0 release. It probably still won't be sorted in any meaningful way, but it will at least be a little more convenient to obtain the keys.

    Regards,
    Mark
  • Thanks for the reply. When is the planned release of 3.6?
  • This is a quick hack that I use, though it doesn't directly address your problem (and it's not recursive as-written) it does make more useful Javascript objects out of Ice Hashmaps:
    HashMapToObject: function(m) {
        var obj={}; 
        m.forEach( function(dn) { 
            obj[dn]=m.get(dn);
        }); 
        return obj
    } 
    
  • mes
    mes California
    This is a quick hack that I use, though it doesn't directly address your problem (and it's not recursive as-written) it does make more useful Javascript objects out of Ice Hashmaps:
    ...
    
    Note that I believe this only works correctly when the map keys are strings.

    Regards,
    Mark