Skip to content

LastOrDefault

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

Enumerable:lastOrDefault

Returns the last value in an iterable, or a default when there is no match.

Syntax

value = values:lastOrDefault(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 last value, the last matching value, or defaultValue when none exists.

Remarks

Pass nil as the predicate to provide only a default value. Valid false values are preserved. For dictionary-like tables, iteration order is unspecified.

Example

local finalUpgrade = LuaQ.from({
    { item = "rune helm", tier = 1 },
    { item = "dragon helm", tier = 2 },
})
    :lastOrDefault(function(gear) return gear.tier >= 3 end,
        { item = "No upgrade found" })
-- finalUpgrade.item is "No upgrade found"

See also

Last · FirstOrDefault · Functions

Clone this wiki locally