Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Go

Idiom #335 List to map

Create the map m containing all the elements e of the list a, using as key the field e.id.

m := make(map[K]V, len(a))
for _, e := range a {
	m[e.id] = e
}

V is the type of the elements of a.
K is the type of the field id.
var m = a.ToDictionary(e => e.id, e => e);

New implementation...
< >
programming-idioms.org