Saturday, June 27, 2020

Flatten a Javascript object

https://stackoverflow.com/questions/19098797/fastest-way-to-flatten-un-flatten-nested-json-objects
https://www.npmjs.com/package/flat

Take a nested Javascript object and flatten it, or unflatten an object with delimited keys.
var flatten = require('flat')
 
flatten({
    key1: {
        keyA: 'valueI'
    },
    key2: {
        keyB: 'valueII'
    },
    key3: { a: { b: { c: 2 } } }
})
 
// {
//   'key1.keyA': 'valueI',
//   'key2.keyB': 'valueII',
//   'key3.a.b.c': 2
// }

No comments:

Post a Comment

Followers

Blog Archive