Standard Library: Obj
Work with objects (key-value maps)
For more information on objects see the language reference.
Module Contents
Obj.focus(object, keys)
Reorder the entries in object so that the given
keys appear first, in the given order.
Obj.focus({ name: "Lamar", yards: 4172, tds: 41, ints: 4 }, ["name", "tds"])
{ name: "Lamar", tds: 41, yards: 4172, ints: 4 }
Obj.get(object, key)
Get the value corresponding to key in
object.
Obj.get({ q: 9, r: 5, b: 3, n: 3, p: 1 }, "q")
9
Note that this can also be accomplished using the index
[] operator:
{ q: 9, r: 5, b: 3, n: 3, p: 1 }["q"]
9
Obj.getDefault(object, key, default)
Get the value corresponding to key in
object, or default if
object does not have key.
capitals = { Maine: "Augusta", Maryland: "Annapolis", Massachusetts: "Boston" }
Obj.getDefault(capitals, "Massachusetts", "idk")
Obj.getDefault(capitals, "Massocheichei", "idk")
"Boston"
"idk"
Obj.has(object, key)
Check whether object contains key.
Obj.has({ q: 9, r: 5, b: 3, n: 3, p: 1 }, "q")
Obj.has({ q: 9, r: 5, b: 3, n: 3, p: 1 }, "k")
true
false
Note that this can also be accomplished using the
in operator.
"q" in { q: 9, r: 5, b: 3, n: 3, p: 1 }
"k" in { q: 9, r: 5, b: 3, n: 3, p: 1 }
true
false
Obj.isEmpty(object)
Check whether object is empty.
len({ q: 9, r: 5, b: 3, n: 3, p: 1 })
len({})
5
0
(Accessible as a global through Overloads.isEmpty)
Obj.keys(object)
Get the keys in object as a list.
Obj.keys({ q: 9, r: 5, b: 3, n: 3, p: 1 })
["q", "r", "b", "n", "p"]
Obj.len(object)
Return the number of entries in object.
len({ q: 9, r: 5, b: 3, n: 3, p: 1 })
5
(Accessible as a global through Overloads.len)
Obj.matches(object, matcher)
Check whether object
matches the object
matcher.
ducky = { name: "Ducky", type: "dog", age: 9 }
Obj.matches(ducky, { name: "Ducky", age: 9 })
Obj.matches(ducky, { name: "Ducky", type: "cat" })
route = { from: { city: "philadelphia" }, to: { city: "baltimore" } }
Obj.matches(route, { from: { city: "philadelphia" } })
Obj.matches(route, { from: { city: "pittsburg" } })
true
false
true
false
Obj.merge(objects)
Merge (flatten) a list of objects into a single object.
o1 = { "Hello": "Ola", "Good afternoon": "Boa tarde" }
o2 = { "Hello": "E aí?", "Good night": "Boa noite" }
Obj.merge([o1 + o2])
{
"Hello": "E aí?",
"Good afternoon": "Boa tarde",
"Good night": "Boa noite",
}
Note that objects can also be merged using the
+ operator.
o1 + o2
{
"Hello": "E aí?",
"Good afternoon": "Boa tarde",
"Good night": "Boa noite",
}
Obj.of(value)
Convert value to an object, where value is
either a table or an object. If value is a table,
return an object whose keys are column names and values are lists of
the column values in table. If value is an
object then return it.
cities = Table.of([
{ city: "New York", state: "NY" },
{ city: "Los Angeles", state: "CA" },
{ city: "Chicago", state: "IL" },
{ city: "Houston", state: "TX" },
])
Obj.of(cities)
{
city: ["New York", "Los Angeles", "Chicago", "Houston"],
state: ["NY", "CA", "IL", "TX"],
}
Obj.product(object)
Get the cartesian product of the entries in object.
Given an object, convert each non-list value to a list
of length 1. Then create a table containing a row for
each possible combination of items from the lists of values. The
keys of objects must be strings.
Obj.product({
rank: ["A", "2", "3", "4" ],
symbol: ["♣", "♦", "♥", "♠"],
count: 1,
})
┌──────┬────────┬───────┐
│ rank │ symbol │ count │ x 16
├──────┼────────┼───────┤
│ A │ ♣ │ 1 │
│ A │ ♦ │ 1 │
│ A │ ♥ │ 1 │
│ A │ ♠ │ 1 │
│ "2" │ ♣ │ 1 │
│ "2" │ ♦ │ 1 │
│ "2" │ ♥ │ 1 │
│ "2" │ ♠ │ 1 │
│ "3" │ ♣ │ 1 │
│ "3" │ ♦ │ 1 │
│ "3" │ ♥ │ 1 │
│ "3" │ ♠ │ 1 │
│ "4" │ ♣ │ 1 │
│ "4" │ ♦ │ 1 │
│ "4" │ ♥ │ 1 │
│ "4" │ ♠ │ 1 │
└──────┴────────┴───────┘
Note that this is equivalent to:
Table.product([
Table.of({ rank: ["A", "2", "3", "4" ] }),
Table.of({ symbol: ["♣", "♦", "♥", "♠"] }),
Table.of({ count: 1 }),
])
Obj.remove(object, key)
Remove the entry with key from object.
remove(
{ name: "Lamar", yards: 4172, tds: 41, ints: 4, fumbles: 10 },
"fumbles"
)
{ name: "Lamar", yards: 4172, tds: 41, ints: 4 }
(Accessible as a global through Overloads.remove)
Obj.removeAll(object, keys)
Remove the entries with the given keys from
object.
Obj.removeAll(
{ name: "Lamar", yards: 4172, tds: 41, ints: 4, fumbles: 10 },
["ints", "fumbles"]
)
{ name: "Lamar", yards: 4172, tds: 41 }
Obj.rename(object, oldToNew)
For every old: new entry in the object
oldToNew, rename the entry in object with
the key old to have the name new.
Obj.rename(
{ name: "Lamar", yards: 4172, tds: 41, ints: 4 },
{ tds: "touchdowns", ints: "interceptions" }
)
{ name: "Lamar", yards: 4172, touchdowns: 41, interceptions: 4 }
Obj.select(object, keys)
Get a subset of the entries in object with the given
keys. Entries will appear in the order given in
keys.
select({ name: "Lamar", yards: 4172, tds: 41, ints: 4 }, ["name", "tds"])
{ name: "Lamar", tds: 41 }
(Accessible as a global through Overloads.select)
Obj.set(object, key, value)
Assign key to value in
object.
phrases = { "Hello": "Ola", "Good afternoon": "Boa tarde" }
Obj.set(phrases, "Good night", "Boa noite")
{ "Hello": "Ola", "Good afternoon": "Boa tarde", "Good night": "Boa noite" }
Note that if you want to update an existing variable, you could also use variable assignment:
phrases = { "Hello": "Ola", "Good afternoon": "Boa tarde" }
phrases["Good night"] = "Boa noite"
phrases ={ "Hello": "Ola", "Good afternoon": "Boa tarde", "Good night": "Boa noite" }
Obj.setDefault(object, key, value)
Assign key to value in
object only if key is not already present.
phrases = { "Hello": "Ola", "Good afternoon": "Boa tarde" }
Obj.setDefault(phrases, "Hello", "E aí")
Obj.setDefault(phrases, "Good night", "Boa noite")
{ "Hello": "Ola", "Good afternoon": "Boa tarde" }
{ "Hello": "Ola", "Good afternoon": "Boa tarde", "Good night": "Boa noite" }
Obj.values(object)
Get the values in object as a list.
Obj.values({ q: 9, r: 5, b: 3, n: 3, p: 1 })
[9, 5, 3, 3, 1]