-
-
Save raiscui/6de10e7da99dfd2b039a1f7976ccb0de to your computer and use it in GitHub Desktop.
This file contains 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
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