Work in progress
A Pen by Victor Nystad on CodePen.
Work in progress
A Pen by Victor Nystad on CodePen.
// assumes either strings or objects | |
var obj = { | |
axis: { | |
x: { | |
position: 'left', | |
categories: { | |
name: 'test-kategori' | |
} | |
}, | |
y : { | |
position: 'bottom' | |
} | |
} | |
}; | |
var newObj = {}; | |
function setParent(o, path) { | |
if (typeof o == 'object') { | |
Object.keys(o).filter(function(key) { | |
return key !== 'path'; | |
}).forEach(function(key) { | |
if (typeof o[key] === 'string') { | |
newObj[path + '-' + key] = o[key]; | |
} else { | |
o[key].path = path ? path + '-' + key : key; | |
setParent(o[key], o[key].path); | |
} | |
}); | |
} | |
}; | |
setParent(obj); | |
console.log(obj, newObj); | |
Object {axis-x-position: "left", axis-x-categories-name: "test-kategori", axis-y-position: "bottom"}