at(index): Lens
ts
at: <A>(index: number) => Lens<A, A[]>;
ts
at: <A>(index: number) => Lens<A, A[]>;
This combinator takes an index and, when passed to derive
, returns an optic focused on the element at the index in the focused array.
Just like JavaScript's Array.at, a negative index counts back from the end of the array.
ts
import {at } from "@optics/react/combinators";constnumbersOptic =createState ([78, 90, 4, 7, 10, 789, 42, 90]);constfifthElementOptic =numbersOptic .derive (at (4));fifthElementOptic .get (); // 10numbersOptic .derive (at (-2)).get (); // 42
ts
import {at } from "@optics/react/combinators";constnumbersOptic =createState ([78, 90, 4, 7, 10, 789, 42, 90]);constfifthElementOptic =numbersOptic .derive (at (4));fifthElementOptic .get (); // 10numbersOptic .derive (at (-2)).get (); // 42