Dict
t
type t<'a> = Js.Dict.t<'a>
Type representing a dictionary of value 'a
.
get
let get: (t<'a>, string) => option<'a>
Returns the value at the provided key, if it exists. Returns an option.
Examples
RESCRIPTlet dict = Dict.fromArray([("someKey", "someValue")])
switch dict->Dict.get("someKey") {
| None => Console.log("Nope, didn't have the key.")
| Some(value) => Console.log(value)
}
set
let set: (t<'a>, string, 'a) => unit
set(dictionary, key, value)
sets the value at the provided key to the provided value.
Examples
RESCRIPTlet dict = Dict.make()
dict->Dict.set("someKey", "someValue")
delete
let delete: (t<'a>, string) => unit
delete(dictionary, key)
deletes the value at key
, if it exists.
Examples
RESCRIPTlet dict = Dict.fromArray([("someKey", "someValue")])
dict->Dict.delete("someKey")
make
let make: unit => t<'a>
make()
creates a new, empty dictionary.
Examples
RESCRIPTlet dict1: Dict.t<int> = Dict.make() // You can annotate the type of the values of your dict yourself if you want
let dict2 = Dict.make() // Or you can let ReScript infer it via usage.
dict2->Dict.set("someKey", 12)
fromArray
let fromArray: array<(string, 'a)> => t<'a>
fromArray(entries)
creates a new dictionary from the provided array of key/value pairs.
Examples
RESCRIPTlet dict = Dict.fromArray([("key1", "value1"), ("key2", "value2")])
fromIterator
let fromIterator: Core__Iterator.t<(string, 'a)> => t<'a>
fromIterator(entries)
creates a new dictionary from the provided iterator of key/value pairs.
Examples
RESCRIPT// Pretend we have an iterator of the correct shape
@val external someIterator: Iterator.t<(string, int)> = "someIterator"
let dict = Dict.fromIterator(someIterator) // Dict.t<int>
toArray
let toArray: t<'a> => array<(string, 'a)>
toArray(dictionary)
returns an array of all the key/value pairs of the dictionary.
Examples
RESCRIPTlet dict = Dict.make()
dict->Dict.set("someKey", 1)
dict->Dict.set("someKey2", 2)
let asArray = dict->Dict.toArray
Console.log(asArray) // Logs `[["someKey", 1], ["someKey2", 2]]` to the console
keysToArray
let keysToArray: t<'a> => array<string>
keysToArray(dictionary)
returns an array of all the keys of the dictionary.
Examples
RESCRIPTlet dict = Dict.make()
dict->Dict.set("someKey", 1)
dict->Dict.set("someKey2", 2)
let keys = dict->Dict.keysToArray
Console.log(keys) // Logs `["someKey", "someKey2"]` to the console
valuesToArray
let valuesToArray: t<'a> => array<'a>
valuesToArray(dictionary)
returns an array of all the values of the dictionary.
Examples
RESCRIPTlet dict = Dict.make()
dict->Dict.set("someKey", 1)
dict->Dict.set("someKey2", 2)
let values = dict->Dict.valuesToArray
Console.log(values) // Logs `[1, 2]` to the console
assign
let assign: (t<'a>, t<'a>) => t<'a>
assign(dictionary1, dictionary2)
shallowly merges dictionary2 into dictionary1, and returns dictionary1.
Beware this will mutate dictionary1. If you're looking for a way to copy a dictionary, check out Dict.copy
.
Examples
RESCRIPTlet dict1 = Dict.make()
dict1->Dict.set("firstKey", 1)
Console.log(dict1->Dict.keysToArray) // Logs `["firstKey"]`
let dict2 = Dict.make()
dict2->Dict.set("someKey", 2)
dict2->Dict.set("someKey2", 3)
let dict1 = dict1->Dict.assign(dict2)
Console.log(dict1->Dict.keysToArray) // Logs `["firstKey", "someKey", "someKey2"]`
copy
let copy: t<'a> => t<'a>
copy(dictionary)
shallowly copies the provided dictionary to a new dictionary.
Examples
RESCRIPTlet dict = Dict.fromArray([("key1", "value1"), ("key2", "value2")])
let dict2 = dict->Dict.copy
// Both log `["key1", "key2"]` here.
Console.log2(dict->Dict.keysToArray, dict2->Dict.keysToArray)