API / Js / Js_dict

Js_dict

Provide utilities for JS dictionary object.

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

RES
let ages = Js.Dict.fromList(list{("Maria", 30), ("Vinh", 22), ("Fred", 49)})

t

type t<'a>

key

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

type key = string

get

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

RES
Js.Dict.get(ages, "Vinh") == Some(22) Js.Dict.get(ages, "Paul") == None
let get: (t<'a>, key) => option<'a>

unsafeGet

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).

RES
Js.Dict.unsafeGet(ages, "Fred") == 49 Js.Dict.unsafeGet(ages, "Paul") // returns undefined
let unsafeGet: (t<'a>, key) => 'a

set

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.

RES
Js.Dict.set(ages, "Maria", 31) Js.log(ages == Js.Dict.fromList(list{("Maria", 31), ("Vinh", 22), ("Fred", 49)})) Js.Dict.set(ages, "David", 66) Js.log(ages == Js.Dict.fromList(list{("Maria", 31), ("Vinh", 22), ("Fred", 49), ("David", 66)}))
let set: (t<'a>, key, 'a) => unit

keys

Returns all the keys in the dictionary dict.

RES
Js.Dict.keys(ages) == ["Maria", "Vinh", "Fred"]
let keys: t<'a> => array<string>

empty

Returns an empty dictionary.

let empty: unit => t<'a>

unsafeDeleteKey

Experimental internal function

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

entries

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

RES
Js.Dict.entries(ages) == [("Maria", 30), ("Vinh", 22), ("Fred", 49)]
let entries: t<'a> => array<(key, 'a)>

values

Returns the values in the given dictionary (ES2017).

RES
Js.Dict.values(ages) == [30, 22, 49]
let values: t<'a> => array<'a>

fromList

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

RES
let capitals = Js.Dict.fromList(list{("Japan", "Tokyo"), ("France", "Paris"), ("Egypt", "Cairo")})
let fromList: list<(key, 'a)> => t<'a>

fromArray

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

RES
let capitals2 = Js.Dict.fromArray([("Germany", "Berlin"), ("Burkina Faso", "Ouagadougou")])
let fromArray: array<(key, 'a)> => t<'a>

map

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

RES
let prices = Js.Dict.fromList(list{("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(list{("pen", 0.90), ("book", 4.50), ("stapler", 6.30)})
let map: ((. 'a) => 'b, t<'a>) => t<'b>