Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function fromJsonToHtml(input) {
- let arrayOfObjects = JSON.parse(input);
- console.log('<table>');
- let tableHead = '<tr>';
- for (let key in arrayOfObjects[0]) {
- tableHead += `<th>${escaper(key).trim()}</th>`;
- }
- tableHead += '</tr>';
- console.log(tableHead);
- for (let el of arrayOfObjects) {
- let tbody = '<tr>';
- for (let key in el) {
- tbody += `<td>${escaper(el[key])}</td>`;
- }
- tbody += '</tr>';
- console.log(tbody);
- }
- function escaper(value) {
- return value
- .toString()
- .replace(/&/g, '&')
- .replace(/</g, '<')
- .replace(/>/g, '>')
- .replace(/"/g, '"')
- .replace(/'/g, ''');
- }
- console.log('</table>');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement