make-tree
Turn an array of keys into a hierarchy of objects
Last updated 6 years ago by tehshrike .
WTFPL · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install make-tree 
SYNC missed versions from official npm registry.

Turns an array of strings [ 'grandparent', 'parent', 'child' ] into a tree of objects:

{
	grandparent: {
		parent: {
			child: {

			}
		}
	}
}

With one simple function call:

const makeTree = require('make-tree')

const child = makeTree([ 'grandparent', 'parent', 'child' ], {})

makeTree(ary, obj)

  • ary: an array of strings representing keys. If any of those keys does not exist in the hierarchy, they will be created as an empty object.
  • obj: the object to be modified by the creation of new descendants

Returns the final object in the hierarchy represented in the array.

const inputObject = {
	parent: {
		child: {
			baby: 'goo!'
		}
	}
}

const child = makeTree([ 'parent', 'child' ], {parent: {child: {baby: 'goo!'}}})

console.log(child) // => { baby: 'goo!' }

License

WTFPL

Current Tags

  • 2.0.0                                ...           latest (2 years ago)

2 Versions

  • 2.0.0                                ...           2 years ago
  • 1.0.0                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (2)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |