Anonymous user
Associative array/Iteration: Difference between revisions
→{{header|JavaScript}} Removed duplicate entry. Added ES5.1 implementation
(GP) |
|||
Line 986:
=={{header|JavaScript}}==
JavaScript does not have associative arrays until ECMAScript 6 brings Maps.
<lang javascript>var myhash = {}; //
myhash["hello"] = 3;
myhash.world = 6;
myhash["!"] = 9;
//iterate using for..in loop
for (var key in myhash) {▼
//ensure key is in object and not in prototype
▲for (key in myhash) {
console.log("Key is: " +
}
▲ }
}</lang>▼
//iterate using ES5.1 Object.keys() and Array.prototype.Map()
var keys = Object.keys(); //get Array of object keys (doesn't get prototype keys)
keys.map(function (key) {
console.log("Key is: " + key + '. Value is: ' + myhash[key]);
▲});</lang>
=={{header|Jq}}==
|