What is flat?
The flat npm package allows for flattening and unflattening complex nested objects and arrays in JavaScript. It can be used to convert deeply nested objects into a single level object with dot-separated keys, or to expand a flat object with dot-separated keys back into a nested structure. This can be particularly useful when dealing with data that needs to be stored in a format that doesn't support nested structures, such as certain databases, or when you need to simplify the structure of data for processing or transmission.
What are flat's main functionalities?
Flatten
Converts a nested object into a flat object with dot-separated keys. In the code sample, the 'flatten' method is used to transform the 'original' object into a single-level 'flattened' object with keys like 'b.c' and 'b.d.e'.
{"original": {"a": 1, "b": {"c": 2, "d": {"e": 3}}}, "flattened": flat.flatten({"a": 1, "b": {"c": 2, "d": {"e": 3}}})}
Unflatten
Converts a flat object with dot-separated keys back into a nested object. In the code sample, the 'unflatten' method is used to transform the 'flattened' object back into its original nested structure.
{"flattened": {"a": 1, "b.c": 2, "b.d.e": 3}, "unflattened": flat.unflatten({"a": 1, "b.c": 2, "b.d.e": 3})}
Other packages similar to flat
lodash
Lodash is a utility library that offers a wide range of functions for manipulating objects and collections. It includes methods like '_.get' and '_.set' which can be used to manipulate nested paths in objects, similar to how 'flat' can flatten and unflatten objects. However, lodash is a much larger library with a broader scope of functionality beyond just flattening and unflattening.
deepdash
Deepdash is an extension for lodash that adds deep operations on objects and arrays. It provides methods for deep manipulation of nested structures, which can be seen as an alternative to 'flat' for certain use cases. Deepdash focuses on deep operations, while 'flat' specifically focuses on flattening and unflattening objects.
flat
Take a nested Javascript object and flatten it, or unflatten an object with
delimited keys.
Installion
$ npm install flat
Methods
flat.flatten(original, options)
Flattens the object - it'll return an object one level deep, regardless of how
nested the original object was:
var flatten = require('flat').flatten
flatten({
key1: {
keyA: 'valueI'
},
key2: {
keyB: 'valueII'
},
key3: { a: { b: { c: 2 } } }
})
flat.unflatten(original, options)
Flattening is reversible too, you can call flat.unflatten()
on an object:
var unflatten = require('flat').unflatten
unflatten({
'three.levels.deep': 42,
'three.levels': {
nested: true
}
})
Options
delimiter
Use a custom delimiter for (un)flattening your objects, instead of .
.
safe
When enabled, both flat
and unflatten
will preserve arrays and their
contents. This is disabled by default.
var flatten = require('flat').flatten
flatten({
this: [
{ contains: 'arrays' },
{ preserving: {
them: 'for you'
}}
]
}, {
safe: true
})
object
When enabled, arrays will not be created automatically when using calling
unflatten, like so:
unflatten({
'hello.you.0': 'ipsum',
'hello.you.1': 'lorem',
'hello.other.world': 'foo'
}, { object: true })