{"id":13396700,"url":"https://github.com/sindresorhus/dot-prop","last_synced_at":"2025-05-14T02:10:01.351Z","repository":{"id":25519171,"uuid":"28951059","full_name":"sindresorhus/dot-prop","owner":"sindresorhus","description":"Get, set, or delete a property from a nested object using a dot path","archived":false,"fork":false,"pushed_at":"2024-05-09T20:31:37.000Z","size":88,"stargazers_count":840,"open_issues_count":8,"forks_count":110,"subscribers_count":13,"default_branch":"main","last_synced_at":"2025-05-12T11:06:53.828Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sindresorhus.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/security.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"sindresorhus","open_collective":"sindresorhus","buy_me_a_coffee":"sindresorhus","custom":"https://sindresorhus.com/donate"}},"created_at":"2015-01-08T05:48:39.000Z","updated_at":"2025-05-11T07:53:05.000Z","dependencies_parsed_at":"2024-11-05T18:35:36.891Z","dependency_job_id":"f61b2b0d-61db-4ffd-a7eb-849e743ac1a0","html_url":"https://github.com/sindresorhus/dot-prop","commit_stats":{"total_commits":106,"total_committers":25,"mean_commits":4.24,"dds":0.4528301886792453,"last_synced_commit":"35eda392c035264bed686463ff625a750f6a82df"},"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fdot-prop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fdot-prop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fdot-prop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fdot-prop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sindresorhus","download_url":"https://codeload.github.com/sindresorhus/dot-prop/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254043215,"owners_count":22004911,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T18:01:00.458Z","updated_at":"2025-05-14T02:09:56.332Z","avatar_url":"https://github.com/sindresorhus.png","language":"JavaScript","funding_links":["https://github.com/sponsors/sindresorhus","https://opencollective.com/sindresorhus","https://buymeacoffee.com/sindresorhus","https://sindresorhus.com/donate"],"categories":["工具","JavaScript","Packages","包","others","Miscellaneous","Number"],"sub_categories":["Miscellaneous","其他","杂项"],"readme":"# dot-prop\n\n\u003e Get, set, or delete a property from a nested object using a dot path\n\n## Install\n\n```sh\nnpm install dot-prop\n```\n\n## Usage\n\n```js\nimport {getProperty, setProperty, hasProperty, deleteProperty} from 'dot-prop';\n\n// Getter\ngetProperty({foo: {bar: 'unicorn'}}, 'foo.bar');\n//=\u003e 'unicorn'\n\ngetProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep');\n//=\u003e undefined\n\ngetProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value');\n//=\u003e 'default value'\n\ngetProperty({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\\\.dot');\n//=\u003e 'unicorn'\n\ngetProperty({foo: [{bar: 'unicorn'}]}, 'foo[0].bar');\n//=\u003e 'unicorn'\n\n// Setter\nconst object = {foo: {bar: 'a'}};\nsetProperty(object, 'foo.bar', 'b');\nconsole.log(object);\n//=\u003e {foo: {bar: 'b'}}\n\nconst foo = setProperty({}, 'foo.bar', 'c');\nconsole.log(foo);\n//=\u003e {foo: {bar: 'c'}}\n\nsetProperty(object, 'foo.baz', 'x');\nconsole.log(object);\n//=\u003e {foo: {bar: 'b', baz: 'x'}}\n\nsetProperty(object, 'foo.biz[0]', 'a');\nconsole.log(object);\n//=\u003e {foo: {bar: 'b', baz: 'x', biz: ['a']}}\n\n// Has\nhasProperty({foo: {bar: 'unicorn'}}, 'foo.bar');\n//=\u003e true\n\n// Deleter\nconst object = {foo: {bar: 'a'}};\ndeleteProperty(object, 'foo.bar');\nconsole.log(object);\n//=\u003e {foo: {}}\n\nobject.foo.bar = {x: 'y', y: 'x'};\ndeleteProperty(object, 'foo.bar.x');\nconsole.log(object);\n//=\u003e {foo: {bar: {y: 'x'}}}\n```\n\n## API\n\n### getProperty(object, path, defaultValue?)\n\nGet the value of the property at the given path.\n\nReturns the value if any.\n\n### setProperty(object, path, value)\n\nSet the property at the given path to the given value.\n\nReturns the object.\n\n### hasProperty(object, path)\n\nCheck whether the property at the given path exists.\n\nReturns a boolean.\n\n### deleteProperty(object, path)\n\nDelete the property at the given path.\n\nReturns a boolean of whether the property existed before being deleted.\n\n### escapePath(path)\n\nEscape special characters in a path. Useful for sanitizing user input.\n\n```js\nimport {getProperty, escapePath} from 'dot-prop';\n\nconst object = {\n\tfoo: {\n\t\tbar: '👸🏻 You found me Mario!',\n\t},\n\t'foo.bar' : '🍄 The princess is in another castle!',\n};\nconst escapedPath = escapePath('foo.bar');\n\nconsole.log(getProperty(object, escapedPath));\n//=\u003e '🍄 The princess is in another castle!'\n```\n\n### deepKeys(object)\n\nReturns an array of every path. Non-empty plain objects and arrays are deeply recursed and are not themselves included.\n\nThis can be useful to help flatten an object for an API that only accepts key-value pairs or for a tagged template literal.\n\n```js\nimport {getProperty, deepKeys} from 'dot-prop';\n\nconst user = {\n\tname: {\n\t\tfirst: 'Richie',\n\t\tlast: 'Bendall',\n\t},\n\tactiveTasks: [],\n\tcurrentProject: null\n};\n\nfor (const property of deepKeys(user)) {\n\tconsole.log(`${property}: ${getProperty(user, property)}`);\n\t//=\u003e name.first: Richie\n\t//=\u003e name.last: Bendall\n\t//=\u003e activeTasks: []\n\t//=\u003e currentProject: null\n}\n```\n\nSparse arrays are supported. In general, [avoid using sparse arrays](https://github.com/sindresorhus/dot-prop/issues/109#issuecomment-1614819869).\n\n#### object\n\nType: `object | array`\n\nObject or array to get, set, or delete the `path` value.\n\nYou are allowed to pass in `undefined` as the object to the `get` and `has` functions.\n\n#### path\n\nType: `string`\n\nPath of the property in the object, using `.` to separate each nested key.\n\nUse `\\\\.` if you have a `.` in the key.\n\nThe following path components are invalid and results in `undefined` being returned: `__proto__`, `prototype`, `constructor`.\n\n#### value\n\nType: `unknown`\n\nValue to set at `path`.\n\n#### defaultValue\n\nType: `unknown`\n\nDefault value.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Fdot-prop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsindresorhus%2Fdot-prop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Fdot-prop/lists"}