Skip to content
Dennis Corvers edited this page Oct 23, 2026 · 1 revision

Enumerable:toMap

Converts values into a Lua table whose keys and values may be selected from each source entry.

Syntax

map = values:toMap(keySelector, elementSelector)

Parameters

Parameter Type Description
keySelector function(value, sourceKey) (optional) Returns the output key. Without it, the source key is used.
elementSelector function(value, outputKey) (optional) Returns the output value. Without it, the original value is used.

Returns

A Lua table mapping each output key to its value.

Remarks

If multiple entries produce the same key, the later value overwrites the earlier value. toDictionary is an alias.

Example

local playersById = LuaQ.from({
    { id = 41, name = "Ada" },
    { id = 58, name = "Bryn" },
})
    :toMap(
        function(player) return player.id end,
        function(player) return player.name end
    )
-- playersById[58] is "Bryn"

See also

ToLookup · ToSet · Functions

Clone this wiki locally