flat
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Flat is a powerful JavaScript library that provides a simple way to flatten and unflatten deeply nested objects. This capability can simplify complex nested structures into a single-level object representation, or vice versa. This functionality proves especially useful when handling complex JSON objects or building data transformation pipelines in JavaScript.
You can use the flat
library in a JavaScript project by first installing it with npm using npm install flat
. After installation, you can import the flatten
and unflatten
functions from it.
Below are example usages:
Flattening an object:
import { flatten } from 'flat'
const result = flatten({
key1: {
keyA: 'valueI'
},
key2: {
keyB: 'valueII'
},
key3: { a: { b: { c: 2 } } }
})
console.log(result);
// Outputs: { 'key1.keyA': 'valueI', 'key2.keyB': 'valueII', 'key3.a.b.c': 2 }
Unflattening an object:
import { unflatten } from 'flat'
const result = unflatten({
'three.levels.deep': 42,
'three.levels': {
nested: true
}
})
console.log(result);
// Outputs: { three: { levels: { deep: 42, nested: true } } }
The official documentation for flat
can be found in the README file of its GitHub repository. Here you can find comprehensive details on installation, usage, options for customization, and examples. You can also learn about its command-line usage for directly dealing with JSON files.