{"record":{"id":"8a7a30dd4714a316","repo":"dotnet/AspNetCore.Docs","slug":"there-already-is-an-object-with-the-key-that-you-s","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/2.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/2.x/TestingControllersSample/src/TestingControllersSample/wwwroot/js/knockout.mapping.js#L524-L560","documentation":"knockout.mapping augments mapped observableArrays with keyed helpers (mappedIndexOf, mappedCreate). mappedCreate enforces uniqueness: if mappedIndexOf(value) !== -1, an item with that key already exists and it throws rather than create a duplicate. The key is derived via the configured `key` callback.","triggerScenarios":"Calling viewModel.mappedCreate(newItem) when an item with the same key already exists in the array; server pushing a duplicate id; key callback returning a colliding value.","commonSituations":"REST endpoints returning the same record twice; optimistic UI creating then receiving a server echo; key function ignoring a sub-field so distinct items collide.","solutions":["Check before creating: if (viewModel.mappedIndexOf(newItem) === -1) viewModel.mappedCreate(newItem);","Ensure the `key` callback produces unique keys for distinct items.","Dedupe incoming server data before mapping.","If duplicates are legitimate, switch to viewModel.push() instead of mappedCreate."],"exampleFix":"// before\nviewModel.mappedCreate(newItem); // throws if key exists\n// after\nif (viewModel.mappedIndexOf(newItem) === -1) {\n  viewModel.mappedCreate(newItem);\n}","handlingStrategy":"validation","validationCode":"// Dedupe before mappedCreate.\nfunction safeMappedCreate(arr, value) {\n  if (arr.mappedIndexOf(value) !== -1) {\n    console.warn('Duplicate key; skipping mappedCreate', value);\n    return;\n  }\n  return arr.mappedCreate(value);\n}","typeGuard":"function hasUniqueKey(arr, value) { return arr.mappedIndexOf(value) === -1; }","tryCatchPattern":"try {\n  viewModel.mappedCreate(newItem);\n} catch (e) {\n  if (/already is an object with the key/.test(e.message)) {\n    console.warn('Item already present; updating instead');\n    var idx = viewModel.mappedIndexOf(newItem);\n    if (idx !== -1) viewModel()[idx](newItem);\n  } else { throw e; }\n}","preventionTips":["Always call mappedIndexOf before mappedCreate.","Ensure the `key` callback yields unique, stable keys.","Dedupe server payloads before mapping them into the array."],"tags":["knockout","knockout-mapping","duplicate-key","observable-array"],"backgroundTag":null,"analyzedSha":"c67a80103a1a74db20784debd919c7fdda96c510","analyzedAt":"2026-08-13T17:46:11.763Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}