{"record":{"id":"08385890bf7a6b1b","repo":"sveltejs/svelte","slug":"each-key-duplicate","errorCode":"each_key_duplicate","errorMessage":"each_key_duplicate\n${value ? `Keyed each block has duplicate key `${value}` at indexes ${a} and ${b}` : `Keyed each block has duplicate key at indexes ${a} and ${b}`}\nhttps://svelte.dev/e/each_key_duplicate","messagePattern":"each_key_duplicate\n(.+?)` at indexes (.+?) and (.+?)` : `Keyed each block has duplicate key at indexes (.+?) and (.+?)`\\}\nhttps://svelte\\.dev/e/each_key_duplicate","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/svelte/src/internal/client/errors.js","lineNumber":146,"sourceCode":"\n/**\n * Keyed each block has duplicate key `%value%` at indexes %a% and %b%\n * @param {string} a\n * @param {string} b\n * @param {string | undefined | null} [value]\n * @returns {never}\n */\nexport function each_key_duplicate(a, b, value) {\n\tif (DEV) {\n\t\tconst error = new Error(`each_key_duplicate\\n${value\n\t\t\t? `Keyed each block has duplicate key \\`${value}\\` at indexes ${a} and ${b}`\n\t\t\t: `Keyed each block has duplicate key at indexes ${a} and ${b}`}\\nhttps://svelte.dev/e/each_key_duplicate`);\n\n\t\terror.name = 'Svelte error';\n\n\t\tthrow error;\n\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/each_key_duplicate`);\n\t}\n}\n\n/**\n * Keyed each block has key that is not idempotent — the key for item at index %index% was `%a%` but is now `%b%`. Keys must be the same each time for a given item\n * @param {string} index\n * @param {string} a\n * @param {string} b\n * @returns {never}\n */\nexport function each_key_volatile(index, a, b) {\n\tif (DEV) {\n\t\tconst error = new Error(`each_key_volatile\\nKeyed each block has key that is not idempotent — the key for item at index ${index} was \\`${a}\\` but is now \\`${b}\\`. Keys must be the same each time for a given item\\nhttps://svelte.dev/e/each_key_volatile`);\n\n\t\terror.name = 'Svelte error';\n\n\t\tthrow error;\n\t} else {","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/sveltejs/svelte/blob/20b341f10048cf1016a2028ac7eee5595cfef6a5/packages/svelte/src/internal/client/errors.js#L128-L164","documentation":"Runtime error `each_key_duplicate`: a keyed `{#each items as item (key)}` block produced the same key value for two different items at the same indexes `a` and `b`. Svelte keys must be unique per item so the diff/patch logic can track elements. The key value is included when available, omitted otherwise.","triggerScenarios":"At runtime (DEV) when the keyed-each reconciler detects that two items in the same list yield identical keys. Common with `(item.id)` when IDs collide, or with `(index)`-style keys when items reorder.","commonSituations":"Backend returning duplicate IDs in a list; using a non-unique field (e.g. `(item.status)`) as the key; merging two lists that share keys; using array index as key while inserting/removing items.","solutions":["Use a truly unique key — typically the item's primary key (`(item.id)`).","If the data has no unique field, derive one (e.g. compose `(item.type + '-' + item.id)`) or sanitize duplicates upstream.","Avoid `(index)` as the key when the list can be reordered, inserted into, or filtered."],"exampleFix":"{#each items as item (item.status)} <!-- duplicate statuses -->\n\n{#each items as item (item.id)} <!-- unique -->","handlingStrategy":"validation","validationCode":"// Detect non-unique keys in a list before render (data-side check).\nfunction findDuplicateKeys(items, keyFn) {\n  const seen = new Map();\n  const dups = [];\n  for (let i = 0; i < items.length; i++) {\n    const k = keyFn(items[i]);\n    if (seen.has(k)) dups.push({ key: k, a: seen.get(k), b: i });\n    else seen.set(k, i);\n  }\n  return dups;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Key `{#each}` on a guaranteed-unique field (typically the entity ID).","Validate list data upstream to reject duplicate IDs before render.","Never key on array index when the list can be reordered or filtered."],"tags":["svelte5","runtime","each","keyed-list","rendering"],"backgroundTag":null,"analyzedSha":"20b341f10048cf1016a2028ac7eee5595cfef6a5","analyzedAt":"2026-08-12T23:37:30.399Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}