Dev Helpers
← Back

Insert at a known position

Insert at a known position using TypeScript.

Open this example in the interactive library

Source code

type Item = Readonly<{ id: number; label: string }>;
const items: readonly Item[] = [
  { id: 1, label: "one" },
  { id: 2, label: "two" },
];
console.log(items.toSpliced(1, 0, { id: 3, label: "inserted" }));

Sample output

[
  { id: 1, label: 'one' },
  { id: 3, label: 'inserted' },
  { id: 2, label: 'two' }
]