Home Help Center

iterate through javascript hash map in key order

alversonalverson Member Dennis AlversonOrganization: Braxton TechnologiesProject: Orbital Analysis
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

  • mesmes CaliforniaAdministrators, ZeroC Staff Mark SpruiellOrganization: ZeroC, Inc.Project: Ice Developer ZeroC Staff
    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
  • alversonalverson Member Dennis AlversonOrganization: Braxton TechnologiesProject: Orbital Analysis
    Thanks for the reply. When is the planned release of 3.6?
  • bmaranvillebmaranville Member Brian MaranvilleOrganization: National Institute of Standards and TechnologyProject: NICE instrument control
    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
    } 
    
  • mesmes CaliforniaAdministrators, ZeroC Staff Mark SpruiellOrganization: ZeroC, Inc.Project: Ice Developer ZeroC Staff
    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
Sign In or Register to comment.