Skip to content

Instantly share code, notes, and snippets.

@jcardoz
Created August 6, 2018 18:33
Show Gist options
  • Save jcardoz/dbda11f028d34e5402a95fbea3c6133b to your computer and use it in GitHub Desktop.
Save jcardoz/dbda11f028d34e5402a95fbea3c6133b to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/tizokan
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
var myOb = {
'hello-_world': "hi",
"name-is": "jonathan",
"age-is": 30,
"lives-_--in": "India",
job:
{
"job-desc": "consultant",
"job--name": "FrontEnd Developer"
},
hobbies: {
"my-hobbies": {
"play-ing": "football",
"read-ing": "sci-fi"
}
}
};
function iter(o) {
Object.keys(o).forEach(function (k) {
// object
if (o[k] !== null && typeof o[k] === 'object') {
setNewNamedProp(k,o[k],o);
iter(o[k]);
if(k.indexOf('-') !== -1) delete o[k];
return o;
}
// string
setNewNamedProp(k,o[k],o);
if(k.indexOf('-') !== -1) delete o[k];
});
}
function setNewNamedProp(propName, value, obj) {
obj[propName.toString().replace(/-/g, '_')] = value;
}
iter(myOb);
console.log(myOb);
</script>
<script id="jsbin-source-javascript" type="text/javascript">var myOb = {
'hello-_world': "hi",
"name-is": "jonathan",
"age-is": 30,
"lives-_--in": "India",
job:
{
"job-desc": "consultant",
"job--name": "FrontEnd Developer"
},
hobbies: {
"my-hobbies": {
"play-ing": "football",
"read-ing": "sci-fi"
}
}
};
function iter(o) {
Object.keys(o).forEach(function (k) {
// object
if (o[k] !== null && typeof o[k] === 'object') {
setNewNamedProp(k,o[k],o);
iter(o[k]);
if(k.indexOf('-') !== -1) delete o[k];
return o;
}
// string
setNewNamedProp(k,o[k],o);
if(k.indexOf('-') !== -1) delete o[k];
});
}
function setNewNamedProp(propName, value, obj) {
obj[propName.toString().replace(/-/g, '_')] = value;
}
iter(myOb);
console.log(myOb);</script></body>
</html>
var myOb = {
'hello-_world': "hi",
"name-is": "jonathan",
"age-is": 30,
"lives-_--in": "India",
job:
{
"job-desc": "consultant",
"job--name": "FrontEnd Developer"
},
hobbies: {
"my-hobbies": {
"play-ing": "football",
"read-ing": "sci-fi"
}
}
};
function iter(o) {
Object.keys(o).forEach(function (k) {
// object
if (o[k] !== null && typeof o[k] === 'object') {
setNewNamedProp(k,o[k],o);
iter(o[k]);
if(k.indexOf('-') !== -1) delete o[k];
return o;
}
// string
setNewNamedProp(k,o[k],o);
if(k.indexOf('-') !== -1) delete o[k];
});
}
function setNewNamedProp(propName, value, obj) {
obj[propName.toString().replace(/-/g, '_')] = value;
}
iter(myOb);
console.log(myOb);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment