replaceOrAppend
Returns a new array with the first item matching predicate replaced by
item — or item appended at the end if no match is found. The common
“upsert into a list” pattern.
Available since v3.0.0
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
array | readonly T[] | null | undefined | The source array. `null`/`undefined` are treated as empty. |
item | T | The replacement (or new) item |
predicate | function | Called with each existing item to find what to replace |
Returns
Section titled “Returns”T[] — A new array with item upserted
Examples
Section titled “Examples”Upsert an item into a list
Section titled “Upsert an item into a list”Replaces the first match, or appends when nothing matches.
Appends when no item matches
Section titled “Appends when no item matches”A missing id is added at the end rather than silently dropped.
