This is the latest docs version
Quick Links
  • -Overview
  • -Language Features
  • -JS Interop
  • -Build System
Documentation
Language Manual
Reference for all language features
ReScript & React
First class bindings for ReactJS
GenType
Seamless TypeScript integration
Reanalyze
Dead Code & Termination analysis
Exploration
Packages
Explore third party libraries and bindings
Syntax Lookup
Discover all syntax constructs
APIPlaygroundBlogCommunity
  • Playground
  • Blog
  • Twitter
  • GitHub
  • Forum
Js Module
Overview
Js
submodules
  • Array
  • Array2
  • BigInt
  • Blob
  • Console
  • Date
  • Dict
    • t
      t
    • t
      key
    • v
      get
    • v
      unsafeGet
    • v
      set
    • v
      keys
    • v
      empty
    • v
      unsafeDeleteKey
    • v
      entries
    • v
      values
    • v
      fromList
    • v
      fromArray
    • v
      map
  • Exn
  • File
  • Float
  • Global
  • Int
  • Json
    • Kind
  • List
  • Map
  • Math
  • Null
  • Null_undefined
  • Nullable
  • Obj
  • Option
  • Promise
  • Promise2
  • Re
  • Result
  • Set
  • String
  • String2
  • TypedArray2
    • DataView
    • Float64Array
    • Float32Array
    • Uint32Array
    • Int32Array
    • Uint16Array
    • Int16Array
    • Uint8ClampedArray
    • Uint8Array
    • Int8Array
    • ArrayBuffer
    Typed_array
    • DataView
    • Float64_array
    • Float64Array
    • Float32_array
    • Float32Array
    • Uint32Array
    • Int32_array
    • Int32Array
    • Uint16Array
    • Int16Array
    • Uint8ClampedArray
    • Uint8Array
    • Int8Array
    • S
    • ArrayBuffer
    • Type
  • Types
  • Undefined
  • Vector
  • WeakMap
  • WeakSet
  • API / Js / Dict

    Dict

    Provide utilities for JS dictionary object.

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

    Examples

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

    t

    RESCRIPT
    type t<'a> = dict<'a>

    key

    RESCRIPT
    type key = string

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

    get

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

    Examples

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

    unsafeGet

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

    Examples

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

    set

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

    Examples

    RESCRIPT
    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)}))

    keys

    RESCRIPT
    let keys: t<'a> => array<string>

    Returns all the keys in the dictionary dict.

    Examples

    RESCRIPT
    Js.Dict.keys(ages) == ["Maria", "Vinh", "Fred"]

    empty

    RESCRIPT
    let empty: unit => t<'a>

    Returns an empty dictionary.

    unsafeDeleteKey

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

    Experimental internal function

    entries

    RESCRIPT
    let entries: t<'a> => array<(key, 'a)>

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

    Examples

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

    values

    RESCRIPT
    let values: t<'a> => array<'a>

    Returns the values in the given dictionary (ES2017).

    Examples

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

    fromList

    RESCRIPT
    let fromList: list<(key, 'a)> => t<'a>

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

    Examples

    RESCRIPT
    let capitals = Js.Dict.fromList(list{("Japan", "Tokyo"), ("France", "Paris"), ("Egypt", "Cairo")})

    fromArray

    RESCRIPT
    let fromArray: array<(key, 'a)> => t<'a>

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

    Examples

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

    map

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

    Examples

    RESCRIPT
    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)})
    Types and values
    • t
      t
    • t
      key
    • v
      get
    • v
      unsafeGet
    • v
      set
    • v
      keys
    • v
      empty
    • v
      unsafeDeleteKey
    • v
      entries
    • v
      values
    • v
      fromList
    • v
      fromArray
    • v
      map

    © 2024 The ReScript Project

    Software and assets distribution powered by KeyCDN.

    About
    • Community
    • ReScript Association
    Find us on