Reason way to add key value pairs that are easily accessible



In Javascript if I wanted to add key-value pairs that I would want to be able to retrieve easily I would start out with an empty object and add key value pairs that I could access easily.

For example:

let obj = {}, i => obj[i] = x)

Then obj[3] would return me exactly what I was expecting.

What would be the ReasonML way to do this since Records are fixed in field names and types.



Hi, do you mean obj[3] would give you what you were expecting? Because arr is the input array, not the new object that you created.

Anyway, it depends on what exact operations you need from this data structure. The example you’ve shown is an array. You can use that without any problems in Reason too.

However if you’re talking about a more general key-value data structure, Reason treats that differently than an array, so you would need to construct and use it differently. E.g.:

let dict = Js.Dict.empty();
Js.Dict.set(dict, "key", "value");
Js.log(dict); // Prints {"key":"value"}

See for documentation


Thanks so much, this is exactly what I was looking for. (You are right about obj[3] and I updated my Q)

I was looking at the Reason docs originally.

Thanks again!