Skip to content

Instantly share code, notes, and snippets.

@vnys
Created October 19, 2014 19:42
Show Gist options
  • Save vnys/154572ad86c52ff6ab43 to your computer and use it in GitHub Desktop.
Save vnys/154572ad86c52ff6ab43 to your computer and use it in GitHub Desktop.
A Pen by Victor Nystad.
// 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);
@vnys
Copy link
Author

vnys commented Oct 19, 2014

Object {axis-x-position: "left", axis-x-categories-name: "test-kategori", axis-y-position: "bottom"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment