Skip to content

Instantly share code, notes, and snippets.

@miroswd
Created September 8, 2021 19:48
Show Gist options
  • Save miroswd/a01c05927313f92a10b748529bb637c5 to your computer and use it in GitHub Desktop.
Save miroswd/a01c05927313f92a10b748529bb637c5 to your computer and use it in GitHub Desktop.
Transform json to string
/**
* @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