Installations
npm install flatted-object
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
14.17.3
NPM Version
7.20.3
Score
68
Supply Chain
97.6
Quality
74.1
Maintenance
100
Vulnerability
98.2
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
mugendi
Download Statistics
Total Downloads
855
Last Day
1
Last Week
1
Last Month
26
Last Year
185
GitHub Statistics
3 Commits
2 Watchers
1 Branches
1 Contributors
Updated on Aug 15, 2021
Bundle Size
5.75 kB
Minified
2.14 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.1.2
Package Id
flatted-object@0.1.2
Unpacked Size
9.75 kB
Size
2.77 kB
File Count
4
NPM Version
7.20.3
Node Version
14.17.3
Total Downloads
Cumulative downloads
Total Downloads
855
Last Day
0%
1
Compared to previous day
Last Week
-87.5%
1
Compared to previous week
Last Month
85.7%
26
Compared to previous month
Last Year
-33.5%
185
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Why?
Flattening objects is somewhat difficult as they could have infinitely many nested levels and some of those levels could be array and even other objects.
This module flattens an object to it's "flattest" size possible while still avoiding collapsing none related keys so that data integrity is maintained.
Why the name?
I love the flatted module and I wanted something that's equally fast and at the same time safe to use. Besides, "flatten-object" was already taken by another module which I didn't like much.
How?
Install yarn add flatted-object
then:
1 2 const flatted = require('flatted-object'); 3 4 let obj = { 5 city: { 6 7 library: { 8 9 books: [{ 10 name: "How to flatten objects like a pro", 11 author: { 12 13 first: "Anthony", 14 second: "Mugendi" 15 } 16 }, 17 { 18 name: "How to flatten objects like a pro", 19 author: { 20 21 first: "Anthony", 22 second: "Mugendi" 23 } 24 } 25 ] 26 } 27 } 28 29 } 30 31 // adding cyclic data 32 obj.obj = obj; 33 34 // use without depthMaps 35 let flattenedObj = flatted(obj); 36 37 console.log(JSON.stringify(flattenedObj, 0, 4)); 38
This will log a flattened object as shown below:
1 2 { 3 "city": [ 4 { 5 "name": "How to flatten objects like a pro", 6 "author": { 7 "first": "Anthony", 8 "second": "Mugendi" 9 } 10 }, 11 { 12 "name": "How to flatten objects like a pro", 13 "author": { 14 "first": "Anthony", 15 "second": "Mugendi" 16 } 17 } 18 ], 19 "obj": "CIRCULAR <obj>" 20 } 21 22
NOTE: There is no way to deal with circular references so they are indicated as "CIRCULAR".
Api
flatten(object, [rootKey]);
You can also pass an alternative rootKey that will be used as the root for the flattened object. Below is an example using the same object above.
1 2 ... 3 4 flattenedObj = flatted(obj, 'city_books'); 5 6 console.log(JSON.stringify(flattenedObj, 0, 4)); 7 8
This will output the following JSON:
1 2 { 3 "city_books": [ 4 { 5 "name": "How to flatten objects like a pro", 6 "author": { 7 "first": "Anthony", 8 "second": "Mugendi" 9 } 10 }, 11 { 12 "name": "How to flatten objects like a pro", 13 "author": { 14 "first": "Anthony", 15 "second": "Mugendi" 16 } 17 } 18 ], 19 "obj": "CIRCULAR <obj>" 20 } 21 22
Also the rootKey allows you to use dot notation to determine how the final structure of the object looks like.
Consider the following:
1 flattenedObj = flatted(obj, 'city.books'); 2 3 console.log(JSON.stringify(flattenedObj, 0, 4)); 4
Which outputs:
1 2 { 3 "city": { 4 "books": [ 5 { 6 "name": "How to flatten objects like a pro", 7 "author": { 8 "first": "Anthony", 9 "second": "Mugendi" 10 } 11 }, 12 { 13 "name": "How to flatten objects like a pro", 14 "author": { 15 "first": "Anthony", 16 "second": "Mugendi" 17 } 18 } 19 ] 20 }, 21 "obj": "CIRCULAR <obj>" 22 } 23 24
Enjoy!

No vulnerabilities found.
Reason
no binaries found in the repo
Reason
1 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
Found 0/3 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
license file not detected
Details
- Warn: project does not have a license file
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
2.5
/10
Last Scanned on 2025-02-10
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More