Skip to content

Instantly share code, notes, and snippets.

@imerkle
Created June 13, 2017 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save imerkle/9ea160838c844473a5f3736d50d1fb19 to your computer and use it in GitHub Desktop.
Save imerkle/9ea160838c844473a5f3736d50d1fb19 to your computer and use it in GitHub Desktop.
const isArray = (a) => {
return (!!a) && (a.constructor === Array);
};
const isObject = (a) => {
return (!!a) && (a.constructor === Object);
};
const gunToObj = (obj,getKey,putKey) => {
getKey = (typeof(getKey) === 'undefined') ? '_title' : getKey;
putKey = (typeof(putKey) === 'undefined') ? 'title' : putKey;
let new_obj = {};
if(isObject(obj)){
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
new_obj[key] = gunToObj(obj[key],getKey,putKey);
}
}
return new_obj;
}else if(isArray(obj)){
for(let i=0;i<obj.length;i++){
if(isObject(obj[i]) && obj[i][getKey]){
new_obj[obj[i][getKey]] = gunToObj(obj[i],getKey,putKey);
}else{
let tmp_obj = {};
tmp_obj[putKey] = obj[i];
new_obj[obj[i]] = tmp_obj;
}
}
return new_obj;
}else{
return obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment