Created
August 6, 2018 18:33
-
-
Save jcardoz/dbda11f028d34e5402a95fbea3c6133b to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/tizokan
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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