https://github.com/toddself/flatten-object
Flatten a nested object, renaming keys if collisions exist
https://github.com/toddself/flatten-object
Last synced: about 1 year ago
JSON representation
Flatten a nested object, renaming keys if collisions exist
- Host: GitHub
- URL: https://github.com/toddself/flatten-object
- Owner: toddself
- Created: 2014-03-17T15:36:59.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2015-04-25T22:26:34.000Z (about 11 years ago)
- Last Synced: 2025-04-10T16:08:55.186Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 141 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](http://travis-ci.org/toddself/flatten-object)
# flatten-object
Flattens nested javascript objects into a single level. Enumerates keys that collide.
## Usage
`npm install flatten-object`
```js
> var flattenObject = require('flatten-object');
> var nested = {foo: 'bar', baz: {foo: 'bar'}};
> flattenObject(nested):
{
foo_0: 'bar',
foo_1: 'bar'
};
```
```js
> var flattenObject = require('flatten-object');
> var nested = {foo: 'bar', baz: {foo: 'bar'}};
> flattenObject(nested, 0, '-'):
{
foo-0: 'bar',
foo-1: 'bar'
};
```
## API
### flattenObject(object, maxDepth, separator)
```
/**
* Flatten an object down to a optionally specified maximum depth
* @method exports
* @param {object} obj Object to flatten
* @param {integer} [maxDepth=0] Maximum depth to recurse to. Zero is unlimited.
* @param {string} [sep=_] Separator for keys. If you use this, you must specify maxDepth. It's just how javascript is
* @returns {object} flattened object
*/
```
## License
[MIT license](/LICENSE), Copyright © 2015 Todd Kennedy