SKGreyHound

Untitled

Jan 20th, 2021
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. function fromJSONToHTMLTable(json) {
  2.  
  3. let arr = JSON.parse(json);
  4.  
  5. let outputArr = ["<table>"];
  6. outputArr.push(makeKeyRow(arr));
  7. arr.forEach(obj => outputArr.push(makeValueRow(obj)));
  8. outputArr.push("</table>");
  9.  
  10. console.log(outputArr.join('\n'));
  11.  
  12.  
  13. function makeKeyRow(arr) {
  14. let result = '';
  15. if (arr.length < 3) {
  16. let [name, score] = Object.keys(arr[0]);
  17. result = ` <tr><th>${name}</th><th>${score}</th></tr>`;
  18. } else {
  19. let [name, score, grade] = Object.keys(arr[0]);
  20. result = ` <tr><th>${name}</th><th>${score}</th><th>${grade}</th></tr>`;
  21. }
  22. return result;
  23. }
  24. function makeValueRow(obj) {
  25. let result = '';
  26. let values = Object.values(obj);
  27. if (arr.length < 3) {
  28. let [name, score] = [...values];
  29. result = `<tr><td>${name}</td><td>${score}</td></tr>`;
  30. } else {
  31. let [name, score, grade] = [...values]
  32. result = `<tr><td>${name}</td><td>${score}</td><td>${grade}</td></tr>`;
  33. }
  34.  
  35. return result;
  36. }
  37.  
  38. function escapeHtml(value) {
  39. return value
  40. .replace(/&/g, "&amp;")
  41. .replace(/</g, "&lt;")
  42. .replace(/>/g, "&gt;")
  43. .replace(/"/g, "&quot;")
  44. .replace(/'/g, "&#039;");
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment