Looping through Hashmap in JavaScript

1,551 views
Skip to first unread message

Prvz Khan

unread,
May 21, 2015, 5:32:05 AM5/21/15
to mozill...@googlegroups.com
I am using Rhino 1.7 to run javascript.
I am creating Hashmap using java.util.HashMap.

function fn(x)
{

}

In the above function fn, I am getting this Hashmap as arguments. How can iterate over this hashmap?

Object.keys(x) not giving correct output.

Mike Schwartz

unread,
May 21, 2015, 8:43:04 AM5/21/15
to mozill...@googlegroups.com
From the decafjs AWS module, see the Iterator() use:

function instanceInfo(instance) {
var securityGroups = [];
for (var group in Iterator(instance.getSecurityGroups())) {
securityGroups.push(String(group.getGroupName()));
}
var tags = [];
for (var tag in Iterator(instance.getTags())) {
tags.push({
key : String(tag.getKey()),
value : String(tag.getValue())
});
}

return {
architecture : String(instance.getArchitecture()),
clientToken : String(instance.getClientToken()),
ebsOptimized : !!instance.getEbsOptimized(),
hypervisor : String(instance.getHypervisor()),
amiId : String(instance.getImageId()),
instanceId : String(instance.getInstanceId()),
instanceType : String(instance.getInstanceType()),
keyName : String(instance.getKeyName()),
launchTime : new Date(instance.getLaunchTime().getTime()),
platform : String(instance.getPlatform()),
privateDnsName : String(instance.getPrivateDnsName()),
privateIpAddress : String(instance.getPrivateIpAddress()),
publicDnsName : String(instance.getPublicDnsName()),
publicIpAddress : String(instance.getPublicIpAddress()),
ramdiskId : String(instance.getRamdiskId()),
rootDeviceName : String(instance.getRootDeviceName()),
rootDeviceType : String(instance.getRootDeviceType()),
securityGroups : securityGroups,
state : String(instance.getState().getName()),
stateReason : String(instance.getStateReason() ? instance.getStateReason().getMessage() : null),
stateTransitionReason : String(instance.getStateTransitionReason()),
tags : tags
};
}

--
You received this message because you are subscribed to the Google Groups "mozilla-rhino" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mozilla-rhin...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Prvz Khan

unread,
May 22, 2015, 12:40:48 AM5/22/15
to mozill...@googlegroups.com
Hi mschwartz,

The solution I wanted mustn't include any outside modules. I want native solution of JavaScript runnable by Rhino Engine.

Although I got the solution:
function fn(x) {

        var enteries = modList.entrySet().iterator();

        while (enteries.hasNext()) {
            var ok = enteries.next();
            print('' + ok.getKey() + ' -> ' + ok.getValue() + '\n');
        }

}

Thanks

Prvz Khan

unread,
May 22, 2015, 2:48:17 AM5/22/15
to mozill...@googlegroups.com
Did typo in previous post:

--var enteries = modList.entrySet().iterator();
++
var enteries = x.entrySet().iterator();

Mike Schwartz

unread,
May 22, 2015, 8:25:22 AM5/22/15
to mozill...@googlegroups.com
Iterator() is built into rhino.  It requires no outside modules.


--
Reply all
Reply to author
Forward
0 new messages