{"id":15659178,"url":"https://github.com/watson/flatten-obj","last_synced_at":"2025-05-01T02:25:19.792Z","repository":{"id":25172880,"uuid":"28595966","full_name":"watson/flatten-obj","owner":"watson","description":"Converts an object literal with deeply nested nodes to a simple key/value object","archived":false,"fork":false,"pushed_at":"2017-07-27T07:33:47.000Z","size":24,"stargazers_count":21,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T12:49:10.118Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"webpack-china/awesome-webpack-cn","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/watson.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":null,"support":null}},"created_at":"2014-12-29T15:09:11.000Z","updated_at":"2024-07-03T17:13:11.000Z","dependencies_parsed_at":"2022-08-06T03:15:33.678Z","dependency_job_id":null,"html_url":"https://github.com/watson/flatten-obj","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watson%2Fflatten-obj","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watson%2Fflatten-obj/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watson%2Fflatten-obj/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watson%2Fflatten-obj/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/watson","download_url":"https://codeload.github.com/watson/flatten-obj/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251811717,"owners_count":21647758,"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-10-03T13:15:31.175Z","updated_at":"2025-05-01T02:25:19.757Z","avatar_url":"https://github.com/watson.png","language":"JavaScript","readme":"# flatten-obj\n\nConverts an object literal with deeply nested nodes to a simple\nkey/value object. In other words converts this:\n\n```js\n{\n  foo: 1,\n  bar: {\n    sub1: 2,\n    sub2: {\n      sub3: 3\n    }\n  }\n}\n```\n\nTo this:\n\n```js\n{\n  foo: 1,\n  'bar.sub1': 2,\n  'bar.sub2.sub3': 3\n}\n```\n\n[![Build status](https://travis-ci.org/watson/flatten-obj.svg?branch=master)](https://travis-ci.org/watson/flatten-obj)\n[![Standard - JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)\n\n## Installation\n\n```\nnpm install flatten-obj\n```\n\n## Usage\n\n```js\nvar flatten = require('flatten-obj')()\n\nvar obj = {\n  foo: {\n    bar: 1\n  }\n}\n\n// outputs `{ 'foo.bar': 1 }`\nconsole.log(flatten(obj))\n```\n\n### Blacklist\n\nSome objects migth seem like object literals, but shouldn't be\nflattened. To avoid this, you can supply a list of classes that\nshouldn't be flattened when the object is traversed:\n\n```js\nvar Klass = function () {\n  this.baz = 1\n}\n\nvar flatten = require('flatten-obj')({ blacklist: [Klass] })\n\nvar obj = {\n  foo: {\n    bar: new Klass()\n  }\n}\n\n// outputs `{ 'foo.bar': { baz: 1 } }`\nconsole.log(flatten(obj))\n```\n\n### Custom separator\n\nYou can use a custom separator character to join keys:\n\n```js\nvar flatten = require('flatten-obj')({ separator: '/' })\n\nvar obj = {\n  foo: {\n    bar: 42\n  }\n}\n\n// outputs `{ 'foo/bar': 42 }`\nconsole.log(flatten(obj))\n```\n\n### Leaves\n\nOnly return the leaf nodes\n\n```js\nvar flatten = require('flatten-obj')({ onlyLeaves: true })\n\nvar obj = {\n  sub: {\n    foo: 1,\n    bar: {\n      baz: 2\n    }\n  }\n}\n\n// outputs `{ foo: 1, baz: 2 }`\nconsole.log(flatten(obj))\n```\n\n## Gotchas\n\n### MongoDB data types\n\nMongoDB data types like `ObjectId` or `Timestamp` looks like regular\nobject literals and should be handled with care. So you would normally\nwant to add those to the blacklist:\n\n```js\nvar mongodb = require('mongodb')\nvar flatten = require('flatten-obj')({ blacklist: [\n  mongodb.ObjectID,\n  mongodb.DBRef,\n  mongodb.Timestamp,\n  mongodb.MinKey,\n  mongodb.MaxKey,\n  mongodb.Long\n]})\n```\n\n### Arrays\n\nThis module currenly leaves arrays and their content in place. I.e. the\nkeys `foo` and `bar` in the following object isn't modified:\n\n```js\n{\n  foo: [1, 2, 3],\n  bar: [{ foo: 1 }, { bar: 2 }]\n}\n```\n\nIf you are familiar with MongoDB you know though that it's possible to\nupdate single elements of an array using the dot-notation-syntax.\n\nOpen a pull request or tell me about your use case if you'd like the\nabove object to be converted to:\n\n```js\n{\n  foo: [1, 2, 3],\n  'bar.0.foo': 1,\n  'bar.1.bar': 2\n}\n```\n\n## License\n\nMIT\n","funding_links":[],"categories":["模块","Modules"],"sub_categories":["对象","Object"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwatson%2Fflatten-obj","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwatson%2Fflatten-obj","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwatson%2Fflatten-obj/lists"}