1

I have data like this:

Data =
            "Time": "06:04:01",
             location: canada,
            "user": [
                {
                    "name": "Drake",
                    "email": "[email protected]",
                    "age": "16",
                },
                {
                    "name": "jack",
                    "email": "[email protected]",
                    "age": "28",
                },
                {
                    "name": "peter",
                    "email": "[email protected]",
                    "age": "20",
                },
    ]

I want to print them with console

I want to loop throught the data and print the data in a a table like this . in Node jS

I want to generate file with fs(file System library) and generate html file here... with table tags:

    str ="<table>
    <th>name</th>
    <th>email</th>
    <th>age</th>
    <td>";
        for(let i = 0; i < Data.user.length; i++){
          str+= JSON.stringify(Data.user[i].name);
        }
    str+= "</td><td>"
        for(let i = 0; i < Data.user.length; i++){
          str+= JSON.stringify(Data.user[i].email);
        }
    str+= "</td><td>"
        for(let i = 0; i < Data.user.length; i++){
          str+= JSON.stringify(Data.user[i].age);
        }
 str+= "</td><td></table>"
return str

I am geting data like : this

The table and everthing is okay there is something wrong with the looping or displaying. Please help me how can I print them like this. Thank you

1 Answer 1

1

Please find the below solution:

str ="<table>
    <th>name</th>
    <th>email</th>
    <th>age</th>";
for(let i = 0; i < Data.user.length; i++){
          str+= "<tr><td>"+JSON.stringify(Data.user[i].name)+"</td><td>"+JSON.stringify(Data.user[i].email)+"</td><td>"+JSON.stringify(Data.user[i].age)+"</td></tr>";
        }
    str+= "</table>"
return str
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it si working but due to JSON.stringify(), in the tables cells I am getting data like "drake", :peter with "" in it: How can I remove it? get only drake, peter
you can use replace regex to avoid unnecessary characters, for eg; let value='"drake", :peter' ; value = value.replace(/[":]/g, "");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.