Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input){
- const parsed = JSON.parse(input);
- let outputArr = ['<table>'];
- outputArr.push(makeKeyRow(parsed));
- parsed.forEach(obj=>{
- outputArr.push(makeValueRow(obj))
- });
- outputArr.push('</table>');
- function makeValueRow(obj){
- let valBlock = '<tr>';
- Object.entries(obj).forEach(pair=>{
- let value = pair[1];
- if(typeof value === 'string'){
- value = value.replace(/&/g, '&')
- .replace(/</g, '<')
- .replace(/>/g, '>')
- .replace(/"/g, '"')
- .replace(/'/g, ''')
- }
- valBlock += `<td>${value}</td>`
- });
- valBlock +='</tr>';
- return valBlock;
- }
- function makeKeyRow(arr) {
- let keyRow = '<tr>';
- Object.keys(arr[0]).forEach(key=>
- keyRow += `<th>${key}</th>`
- );
- keyRow += '</tr>';
- return keyRow;
- }
- return outputArr.join('\n');
- }
Advertisement
Add Comment
Please, Sign In to add comment