Skip to content
Flatten or nest JavaScript objects with custom separators (delimiters).
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
test
.gitignore
.npmignore
.travis.yml
LICENSE
README.md
escape.js
flatten.js
index.js
nest.js
package.json
seek.js

README.md

flattenator

npm version npm downloads Build Status

This is a fork of https://github.com/brycebaril/node-flatnest. It provides customizable separators for object keys (default: .) and arrays (default: [ and ]).

Flatten/Nest Javascript objects.

var fn = require("flatnest")

var obj = {
  cat: "meow",
  dog: [{name: "spot"}, {name: "rover"}],
  bird: {type: "parrot", age: 22.3, stats: {weight: 10, height: 15}}
}

var flat = fn.flatten()(obj)

/*
{ cat: 'meow',
  'dog[0].name': 'spot',
  'dog[1].name': 'rover',
  'bird.type': 'parrot',
  'bird.age': 22.3,
  'bird.stats.weight': 10,
  'bird.stats.height': 15 }
 */


var nested = fn.nest()(flat)

/*
{ cat: 'meow',
  dog: [ { name: 'spot' }, { name: 'rover' } ],
  bird:
   { type: 'parrot',
     age: 22.3,
     stats: { weight: 10, height: 15 } } }
 */

// An internal `seek` function is also exposed:

fn.seek()(obj, "bird.stats.height") // 15

API

flatten([separator], [separatorArrayLeft], [separatorArrayRight])(object)

Defaults:

  • separator: .
  • separatorArrayLeft: [
  • separatorArrayRight: ]

Flatten an object to a javascript object with only key: value pairs where values are not complex data types. (e.g. they can be numbers, strings, booleans, but not Objects or Arrays)

Keys are named with paths to where the keys where when nested.

nest([separator], [separatorArrayLeft], [separatorArrayRight])(flatObject)

Defaults:

  • separator: .
  • separatorArrayLeft: [
  • separatorArrayRight: ]

Re-form a flattend object into the nested version. It parses the key paths set during flattening and should end up with the original version. This is not always true depending on what data was present and the original key names chosen.

seek([separator], [separatorArrayLeft], [separatorArrayRight])(object, path)

Defaults:

  • separator: .
  • separatorArrayLeft: [
  • separatorArrayRight: ]

Use the flattened key syntax (e.g. aa.bb[0].cc) to look into a nested object.

You can’t perform that action at this time.