Created
September 8, 2021 19:48
-
-
Save miroswd/a01c05927313f92a10b748529bb637c5 to your computer and use it in GitHub Desktop.
Transform json to string
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
/** | |
* @param {Object[]} arrJson | |
* @returns {String} | |
*/ | |
let transformJsonToString = (arrJson) => { | |
let newObj = "["; | |
let counter = 0; | |
arrJson.forEach((element) => { | |
let str = | |
Object.entries(element) | |
.reduce((a, e) => { | |
if (typeof e[1] != "function") { | |
a += `"${e[0]}" : "${e[1]}", `; | |
} | |
return a; | |
}, "`{") | |
.slice(1, -2) + "}"; | |
counter++; | |
newObj += str; | |
newObj += arrJson.length > counter ? "," : ""; | |
}); | |
newObj += "]"; | |
return newObj; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment