3

Let's say I have an array of type Record[] and I want to create an associative array from it, key being rec.key. Is there an easy way to do it?

Danol
  • 368
  • 1
  • 15

1 Answers1

6

Yes, you can use std.array, std.typecons and std.algorithm libraries and construct this one-liner:

Record[Key] assocArray = array.map!( item => tuple( item.key, item ) ).assocArray;

It takes array, maps it to a tuple (Key, Record) and then takes that list of tuples and creates an associative array from it.

Danol
  • 368
  • 1
  • 15