Dev Helpers
← Back

Update a nested dictionary entry

Update a nested dictionary entry 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(
  (() => {
    const state = { byId: { 1: items[0] } };
    return {
      ...state,
      byId: { ...state.byId, 1: { ...state.byId[1], label: "updated" } },
    };
  })(),
);

Sample output

{ byId: { '1': { id: 1, label: 'updated' } } }