{"record":{"id":"0fd350e0595ea6ed","repo":"dotnet/AspNetCore.Docs","slug":"there-already-is-an-object-with-the-key-that-you-s-0fd350","errorCode":null,"errorMessage":"There already is an object with the key that you specified.","messagePattern":"There already is an object with the key that you specified\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"aspnetcore/mvc/controllers/testing/samples/3.x/TestingControllersSample/src/TestingControllersSample/wwwroot/js/knockout.mapping.js","lineNumber":542,"sourceCode":"\t\t\t\t\tvar arrayOfKeys = filterArrayByKey(arrayOfValues, keyCallback);\n\t\t\t\t\treturn mappedRootObject.destroy(function (item) {\n\t\t\t\t\t\treturn ko.utils.arrayIndexOf(arrayOfKeys, keyCallback(item)) != -1;\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tmappedRootObject.mappedIndexOf = function (item) {\n\t\t\t\t\tvar keys = filterArrayByKey(mappedRootObject(), keyCallback);\n\t\t\t\t\tvar key = keyCallback(item);\n\t\t\t\t\treturn ko.utils.arrayIndexOf(keys, key);\n\t\t\t\t}\n\n\t\t\t\tmappedRootObject.mappedGet = function (item) {\n\t\t\t\t\treturn mappedRootObject()[mappedRootObject.mappedIndexOf(item)];\n\t\t\t\t}\n\n\t\t\t\tmappedRootObject.mappedCreate = function (value) {\n\t\t\t\t\tif (mappedRootObject.mappedIndexOf(value) !== -1) {\n\t\t\t\t\t\tthrow new Error(\"There already is an object with the key that you specified.\");\n\t\t\t\t\t}\n\n\t\t\t\t\tvar item = hasCreateCallback() ? createCallback(value) : value;\n\t\t\t\t\tif (hasUpdateCallback()) {\n\t\t\t\t\t\tvar newValue = updateCallback(item, value);\n\t\t\t\t\t\tif (ko.isWriteableObservable(item)) {\n\t\t\t\t\t\t\titem(newValue);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\titem = newValue;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tmappedRootObject.push(item);\n\t\t\t\t\treturn item;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tvar currentArrayKeys = filterArrayByKey(ko.utils.unwrapObservable(mappedRootObject), keyCallback).sort();\n\t\t\tvar newArrayKeys = filterArrayByKey(rootObject, keyCallback);","sourceCodeStart":524,"sourceCodeEnd":560,"githubUrl":"https://github.com/dotnet/AspNetCore.Docs/blob/c67a80103a1a74db20784debd919c7fdda96c510/aspnetcore/mvc/controllers/testing/samples/3.x/TestingControllersSample/src/TestingControllersSample/wwwroot/js/knockout.mapping.js#L524-L560","documentation":"knockout.mapping.js's `mappedCreate` checks `mappedIndexOf(value) !== -1` and throws if the item's key already exists in the mapped observable array. Each mapped array is keyed by a callback (often an id property), and uniqueness is enforced on create.","triggerScenarios":"Calling `myMappedArray.mappedCreate(newItem)` where `mapKey(newItem)` matches an existing element's key. Also triggered by importing data with duplicate ids into a keyed mapping.","commonSituations":"Server data containing duplicate primary keys; re-running fromJS over an array that already has the items without a key update path; wrong key callback that collapses distinct items to the same key.","solutions":["Before creating, check `if (myMappedArray.mappedIndexOf(newItem) === -1) myMappedArray.mappedCreate(newItem);`.","Use `ko.mapping.fromJS(newData, { key: x => x.id }, myMappedArray)` which updates existing keys in place rather than creating duplicates.","Dedupe the incoming dataset by the key property before mapping.","Verify the key callback returns a genuinely unique value per item."],"exampleFix":"// before\nmyMappedArray.mappedCreate(newItem);\n\n// after\nif (myMappedArray.mappedIndexOf(newItem) === -1) {\n  myMappedArray.mappedCreate(newItem);\n}","handlingStrategy":"validation","validationCode":"function safeCreate(mappedArray, value) {\n  if (mappedArray.mappedIndexOf(value) !== -1) {\n    throw new Error('Duplicate key — use fromJS with a key to update instead');\n  }\n  mappedArray.mappedCreate(value);\n}","typeGuard":"function isUniqueKey(mappedArray, value) {\n  return mappedArray.mappedIndexOf(value) === -1;\n}","tryCatchPattern":"try {\n  myArray.mappedCreate(item);\n} catch (e) {\n  if (/already is an object/.test(e.message)) {\n    ko.mapping.fromJS([item], { key: x => x.id }, myArray);\n  } else throw e;\n}","preventionTips":["Dedupe server data by key before mapping.","Prefer fromJS with a key callback over mappedCreate for upsert semantics.","Verify the key callback returns unique values."],"tags":["knockout","duplicate-key","mapping","validation"],"backgroundTag":null,"analyzedSha":"c67a80103a1a74db20784debd919c7fdda96c510","analyzedAt":"2026-08-13T17:46:11.763Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}