Map
Bindings to the mutable JavaScript Map.
See Map on MDN.
asIterable
let asIterable: t<'k, 'v> => Iterable.t<('k, 'v)>asIterable(map) views map as an Iterable.t of key/value pairs.
clear
let clear: t<'k, 'v> => unitClears all entries in the map.
Examples
RESlet map = Map.make()
map->Map.set("someKey", "someValue")
map->Map.size // 1
map->Map.clear
map->Map.size // 0
delete
let delete: (t<'k, 'v>, 'k) => boolDeletes the provided key and its value from the map. Returns a bool for whether the key existed, and was deleted.
Examples
RESlet map = Map.make()
map->Map.set("someKey", "someValue")
let didDeleteKey = map->Map.delete("someKey")
Console.log(didDeleteKey) // Logs `true` to the console, becuase the map had the key, so it was successfully deleted
let didDeleteKey = map->Map.delete("someNonExistantKey")
Console.log(didDeleteKey) // Logs `false` to the console, becuase the key did not exist
entries
let entries: t<'k, 'v> => IteratorObject.t<('k, 'v), unit, unknown>Returns an iterator that holds all entries of the map.
An entry is represented as a tuple of ('key, 'value),
Examples
RESlet map = Map.make()
map->Map.set("someKey", "someValue")
map->Map.set("anotherKey", "anotherValue")
let entries = map->Map.entries
// Logs the first value
switch entries->IteratorObject.next {
| Yield({value}) => Console.log(value)
| Return(_) => ()
}
// You can also turn the iterator into an array. Remember that iterators are consumed, so use a fresh one.
Console.log(map->Map.entries->IteratorObject.toArray)
forEach
let forEach: (t<'k, 'v>, 'v => unit) => unitIterates through all values of the map.
Please note that this is without the keys, just the values. If you need the key as well, use
Map.forEachWithKey.
Examples
RESlet map = Map.make()
map->Map.set("someKey", "someValue")
map->Map.set("someKey2", "someValue2")
map->Map.forEach(value => {
Console.log(value)
})
forEachWithKey
let forEachWithKey: (t<'k, 'v>, ('v, 'k) => unit) => unitIterates through all values of the map, including the key for each value.
Examples
RESlet map = Map.make()
map->Map.set("someKey", "someValue")
map->Map.set("someKey2", "someValue2")
map->Map.forEachWithKey((value, key) => {
Console.log2(value, key)
})
fromArray
let fromArray: array<('k, 'v)> => t<'k, 'v>Turns an array of key/value pairs into a Map.
Examples
REStype languages = ReScript | JavaScript | TypeScript
let languageRank = [(ReScript, 1), (JavaScript, 2), (TypeScript, 3)]
let map = Map.fromArray(languageRank) // Map.t<languages, int>
switch map->Map.get(ReScript) {
| Some(1) => Console.log("Yay, ReScript is #1!")
| _ => Console.log("Uh-oh, something is _terribly_ wrong with this program... abort.")
}
fromIterable
let fromIterable: Iterable.t<('k, 'v)> => t<'k, 'v>Turns an iterable in the shape of ('key, 'value) into a Map.
Examples
RESlet iterable: Iterable.t<(string, string)> = [("first", "1"), ("second", "2")]->Array.asIterable
iterable
->Map.fromIterable
->Map.size == 2
get
let get: (t<'k, 'v>, 'k) => option<'v>Returns the value for a key, if a value exists at that key.
Examples
RESlet map = Map.make()
map->Map.set("someKey", "someValue")
switch map->Map.get("someKey") {
| None => Console.log("Nope, didn't have it.")
| Some(value) => Console.log2("Yay, had the value, and it's:", value)
}
has
let has: (t<'k, 'v>, 'k) => boolChecks whether the map has a specific key.
Examples
RESlet map = Map.make()
map->Map.set("someKey", "someValue")
switch map->Map.has("someKey") {
| false => Console.log("Nope, didn't have it.")
| true => Console.log("Yay, we have the value!")
}
ignore
let ignore: t<'k, 'v> => unitignore(map) ignores the provided map and returns unit.
This helper is useful when you want to discard a value (for example, the result of an operation with side effects) without having to store or process it further.
isEmpty
let isEmpty: t<'k, 'v> => boolisEmpty(map) returns true if the map has no key/value pairs, false otherwise.
Examples
RESlet emptyMap = Map.make()
emptyMap->Map.isEmpty->assertEqual(true)
let map = Map.make()
map->Map.set("someKey", "someValue")
map->Map.isEmpty->assertEqual(false)
// After clearing the map
map->Map.clear
map->Map.isEmpty->assertEqual(true)
keys
let keys: t<'k, 'v> => IteratorObject.t<'k, unit, unknown>Returns an iterator that holds all keys of the map.
Examples
RESlet map = Map.make()
map->Map.set("someKey", "someValue")
map->Map.set("anotherKey", "anotherValue")
let keys = map->Map.keys
// Logs the first key
switch keys->IteratorObject.next {
| Yield({value}) => Console.log(value)
| Return(_) => ()
}
// You can also turn the iterator into an array. Remember that iterators are consumed, so use a fresh one.
Console.log(map->Map.keys->IteratorObject.toArray)
make
let make: unit => t<'k, 'v>Creates a new, mutable JavaScript Map. A Map can have any values as both keys and values.
See Map on MDN.
Examples
RES`make()`
// You can annotate the type of your map if you want to
let myMap: Map.t<string, int> = Map.make()
// Or you can let ReScript infer what's in your map
let map = Map.make()
map->Map.set("lang", "ReScript") // Inferred as Map.t<string, string>
Alternatives
A JavaScript Map is mutable. For an immutable alternative, install the separate
@rescript/belt package and check out Belt.Map.
set
let set: (t<'k, 'v>, 'k, 'v) => unitSets the provided value to the provided key.
Examples
RESlet map = Map.make()
map->Map.set("someKey", "someValue")
size
let size: t<'k, 'v> => intReturns the size, the number of key/value pairs, of the map.
Examples
RESlet map = Map.make()
map->Map.set("someKey", "someValue")
let size = map->Map.size // 1
t
type t<'k, 'v>Type representing an instance of Map.
values
let values: t<'k, 'v> => IteratorObject.t<'v, unit, unknown>Returns an iterator that holds all values of the map.
Examples
RESlet map = Map.make()
map->Map.set("someKey", "someValue")
map->Map.set("anotherKey", "anotherValue")
let values = map->Map.values
// Logs the first value
switch values->IteratorObject.next {
| Yield({value}) => Console.log(value)
| Return(_) => ()
}
// You can also turn the iterator into an array. Remember that iterators are consumed, so use a fresh one.
Console.log(map->Map.values->IteratorObject.toArray)