Last active
February 10, 2018 23:46
-
-
Save emshan/56bb8376dd25fc49f32fddd52fb61142 to your computer and use it in GitHub Desktop.
object convert or map to string : value js / ts
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
//{ 'c/f/l/k': 1, 'c/f/l/d': 2, 'c/f/w': 2, 'c/d': 1, 'c/e': 2 } | |
//mapStringValue({ | |
// c: { | |
// d:1, | |
// e:2, | |
// f: { | |
// w: 2, | |
// l: { | |
// k:1, | |
// d:2 | |
// } | |
// } | |
// } | |
// }, '') | |
let mapStringValue = (obj: object, prefix: string) => { | |
let keys = Object.keys(obj); | |
let object: any = {}; | |
keys.map(((value, index) => { | |
if (obj[value] !== null && typeof obj[value] === 'object') { | |
let r; | |
if (prefix !== '') | |
r = mapStringValue(obj[value], prefix + value + '/'); | |
else | |
r = mapStringValue(obj[value], value + '/'); | |
object = { | |
...r, | |
...object | |
} | |
} else { | |
object[prefix + value] = obj[value]; | |
} | |
})); | |
return object; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment