API / Js / Dict

You are currently looking at the < v8.2.0 docs (Reason v3.6 syntax edition). You can find the latest API docs here.

(These docs cover all versions between v3 to v8 and are equivalent to the old BuckleScript docs before the rebrand)

Dict

Provide utilities for JS dictionary object.

Note: This module's examples will assume this predeclared dictionary:

RE
let ages = Js.Dict.fromList( [("Maria", 30), ("Vinh", 22), ("Fred", 49)] );

t

RE
type t('a);

Dictionary type (ie an '' JS object). However it is restricted to hold a single type; therefore values must have the same type. This Dictionary type is mostly used with the Js_json.t type.

key

RE
type key = string;

The type for dictionary keys. This means that dictionaries must use strings as their keys.

get

RE
let get: (t('a), key) => option('a);

Js.Dict.get(key) returns None if the key is not found in the dictionary, Some(value) otherwise.

RE
Js.Dict.get(ages, "Vinh") == Some(22); Js.Dict.get(ages, "Paul") == None;

unsafeGet

RE
let unsafeGet: (t('a), key) => 'a;

Js.Dict.unsafeGet(key) returns the value if the key exists, otherwise an undefined value is returned. Use this only when you are sure the key exists (i.e. when having used the keys() function to check that the key is valid).

RE
Js.Dict.unsafeGet(ages, "Fred") == 49; Js.Dict.unsafeGet(ages, "Paul"); // returns undefined

set

RE
let set: (t('a), key, 'a) => unit;

Js.Dict.set(dict, key, value) sets the key/value in the dictionary dict. If the key does not exist, and entry will be created for it. This function modifies the original dictionary.

RE
Js.Dict.set(ages, "Maria", 31) Js.log(ages == Js.Dict.fromList([("Maria", 31), ("Vinh", 22), ("Fred", 49)])); Js.Dict.set(ages, "David", 66) Js.log(ages == Js.Dict.fromList([("Maria", 31), ("Vinh", 22), ("Fred", 49), ("David", 66)]));

keys

RE
let keys: t('a) => array(string);

Returns all the keys in the dictionary dict.

RE
Js.Dict.keys(ages) == [|"Maria", "Vinh", "Fred"|];

empty

RE
let empty: unit => t('a);

Returns an empty dictionary.

unsafeDeleteKey

RE
let unsafeDeleteKey: (. t(string), string) => unit;

Experimental internal function

entries

RE
let entries: t('a) => array((key, 'a));

Returns an array of key/value pairs in the given dictionary (ES2017).

RE
Js.Dict.entries(ages) == [|("Maria", 30), ("Vinh", 22), ("Fred", 49)|]

values

RE
let values: t('a) => array('a);

Returns the values in the given dictionary (ES2017).

RE
Js.Dict.values(ages) == [|30, 22, 49|]

fromList

RE
let fromList: list((key, 'a)) => t('a);

Creates a new dictionary containing each (key, value) pair in its list argument.

RE
let capitals = Js.Dict.fromList([("Japan", "Tokyo"), ("France", "Paris"), ("Egypt", "Cairo")]);

fromArray

RE
let fromArray: array((key, 'a)) => t('a);

Creates a new dictionary containing each (key, value) pair in its array argument.

RE
let capitals2 = Js.Dict.fromArray([|("Germany", "Berlin"), ("Burkina Faso", "Ouagadougou")|]);

map

RE
let map: ((. 'a) => 'b, t('a)) => t('b);

map(f, dict) maps dict to a new dictionary with the same keys, using the function f to map each value.

RE
let prices = Js.Dict.fromList([("pen", 1.00), ("book", 5.00), ("stapler", 7.00)]); let discount = (. price) => {price *. 0.90}; let salePrices = Js.Dict.map(discount, prices); salePrices == Js.Dict.fromList([("pen", 0.90), ("book", 4.50), ("stapler", 6.30)]);