Logo

Programming-Idioms

  • PHP
  • Haskell
  • Js

Idiom #13 Iterate over map keys and values

Access each key k with its value x from an associative array mymap, and print them.

Traversing each key and each associated object of mymap
for (const key in mymap) {
    console.log('key:', key, 'value:', mymap[key]);
}
Object.entries(mymap).forEach(([key, value]) => {
	console.log('key:', key, 'value:', value);
});
foreach ($mymap as $k=>$x)
{
    echo "Key=$k, Value=$x <br>";
}
import Data.List (intercalate)
import qualified Data.Map as Map
let f k v = [show k, " = ", show v]
    mapped = Map.mapWithKeys f mymap
in putStrLn $ intercalate "," $ mapped
with Ada.Containers.Indefinite_Hashed_Maps;
with Ada.Strings.Hash;

use Ada.Containers;
for C in My_Map.Iterate loop
   Put_Line ("Key = " & Key (C) & ", Value = " & Element (C));
end loop;

New implementation...