Created
August 6, 2018 18:38
-
-
Save jcardoz/82da1adb3d74d5d927ac20a234b42dbf 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 iterator(obj) { | |
Object.keys(obj).forEach(function (key) { | |
// object | |
if (obj[key] !== null && typeof obj[key] === 'object') { | |
setNewNamedProp(key,obj[key],obj); | |
iterator(obj[key]); | |
// Delete the key only if it contains a hyphen | |
deleteOldProp(key, obj); | |
return obj; | |
} | |
// string | |
setNewNamedProp(key,obj[key],obj); | |
// Delete the key only if it contains a hyphen | |
deleteOldProp(key, obj); | |
}); | |
} | |
function setNewNamedProp(propName, value, obj) { | |
obj[propName.toString().replace(/-/g, '_')] = value; | |
} | |
function deleteOldProp(propName, obj) { | |
if(propName.indexOf('-') !== -1) delete obj[propName]; | |
} | |
iterator(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 iterator(obj) { | |
Object.keys(obj).forEach(function (key) { | |
// object | |
if (obj[key] !== null && typeof obj[key] === 'object') { | |
setNewNamedProp(key,obj[key],obj); | |
iterator(obj[key]); | |
// Delete the key only if it contains a hyphen | |
deleteOldProp(key, obj); | |
return obj; | |
} | |
// string | |
setNewNamedProp(key,obj[key],obj); | |
// Delete the key only if it contains a hyphen | |
deleteOldProp(key, obj); | |
}); | |
} | |
function setNewNamedProp(propName, value, obj) { | |
obj[propName.toString().replace(/-/g, '_')] = value; | |
} | |
function deleteOldProp(propName, obj) { | |
if(propName.indexOf('-') !== -1) delete obj[propName]; | |
} | |
iterator(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 iterator(obj) { | |
Object.keys(obj).forEach(function (key) { | |
// object | |
if (obj[key] !== null && typeof obj[key] === 'object') { | |
setNewNamedProp(key,obj[key],obj); | |
iterator(obj[key]); | |
// Delete the key only if it contains a hyphen | |
deleteOldProp(key, obj); | |
return obj; | |
} | |
// string | |
setNewNamedProp(key,obj[key],obj); | |
// Delete the key only if it contains a hyphen | |
deleteOldProp(key, obj); | |
}); | |
} | |
function setNewNamedProp(propName, value, obj) { | |
obj[propName.toString().replace(/-/g, '_')] = value; | |
} | |
function deleteOldProp(propName, obj) { | |
if(propName.indexOf('-') !== -1) delete obj[propName]; | |
} | |
iterator(myOb); | |
console.log(myOb); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment