Skip to content

FirstOrDefault

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

Enumerable:firstOrDefault

Returns the first value in an iterable, or a default if no value matches.

Syntax

value = values:firstOrDefault(predicate, defaultValue)

Parameters

Parameter Type Description
predicate function(value, sourceKey) (optional) Returns true for a matching value.
defaultValue any (optional) Value returned when there is no match.

Returns

The first value, the first matching value, or defaultValue when none exists.

Remarks

To provide a default without a predicate, pass nil as the first argument. Valid false values are preserved.

Example

local nextTask = LuaQ.from({
    { name = "Repair armor", priority = 1 },
    { name = "Restock food", priority = 2 },
})
    :firstOrDefault(function(task) return task.priority == 0 end,
        { name = "No urgent task" })
-- nextTask.name is "No urgent task"

See also

First · LastOrDefault · Functions

Clone this wiki locally