Public
Edited
Oct 23, 2024
5 stars
Insert cell
Insert cell
Insert cell
Insert cell
sourceTree = ({
a: 0,
b:[{ sub:{ tab:{ b:[null] } } }],
c: { c: 2, d: 3 }
})
Insert cell
renest({sourceTree})
Insert cell
Insert cell
Insert cell
reduct(sourceTree)
Insert cell
function reduct(obj, prev = null, dict = new Map) {

// ignore these keys in dict
let skip = []
// initialise tree, objects
if (!obj.tree && !prev) {
obj = Object.assign({},{ tree:obj },{ dict })
skip = Object.keys(obj) }

// initialise reducer
return Object.entries(obj).reduce
(( pool, [key, val], i, arr ) => {
// setup custom paths
let path = [prev !== 'TREE' ? prev : '$' ,key.toUpperCase()].join(prev && prev !== 'TREE' ? ':' : '')
// = [prev,key].join(prev ? ':' : '')
// of the form ^

// optional callbacks
if (skip.filter(d=>d==key).length<1)
dict.set(path,`${key} (${has(val, {},[]) ? 'stem' : 'leaf'})`)

// initiate recursion
if (has(val, {},[])) {
pool[path] = reduct(obj[key], path ,dict)
} else { pool[path] = val }
return pool }, {});
}
Insert cell
Insert cell
has = (prop, ...types) =>
types.some((type) =>
Object.prototype.toString.call(prop)
=== Object.prototype.toString.call(type));
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more