Here is my json APi -- >>> https://api.myjson.com/bins/p18mi
I have a table in which I have to print the Json data.
The problem is I want to print the Question data in major Question header and User_info array in thofUser column.
But its not working as expected. I am not able to arrange the table data properly so that all the user details must come User column and sub column. Same for question.
The output i am getting is this is image
$(function() {
var people = [];
$.getJSON('https://api.myjson.com/bins/p18mi', function(data) {
$.each(data.ct_info, function(i, f) {
var tblRow = " <tr>" + `<td id=${f.id}>` + `${f.id}` + "</td>" +
"<td>" + f.name + "</td>";
$(tblRow).appendTo("#userdata tbody");
var users = []
var question = []
f.Qid_info.forEach((x) => {
x.user_info.forEach((y) => {
//avoid duplicates
var foundUser = users.find((user) => {
return user.id === y.id
})
if (!foundUser) {
users.push(y)
}
})
})
f.Qid_info.forEach((x) => {
var foundQuestion = question.find((questions) => {
return questions.id === x.id
})
if (!foundQuestion) {
question.push(x)
}
})
$.each(question, function(i, question) {
var questionRow = `<td id=${question.id}>` + `${question.id}` +
"</td>" + "<td>" + question.isActive + "</td><td>" + question.iscomplex + "</td>" +
"<td>" + question.isbreakdown + "</td>"
$(questionRow).appendTo("#userdata tbody");
})
$.each(users, function(i, user) {
var userRow = `<td id=${user.id}>` + `${user.id}` +
"</td>" + "<td>" + user.name + "</td><td>" + user.data + "</td>" +
"<td>" + user.updatedAt + "</td>"
$(userRow).appendTo("#userdata tbody");
})
});
});
});
#user {
overflow-x: auto;
white-space: nowrap;
}
th,
td {
font-weight: normal;
padding: 5px;
text-align: center;
width: 120px;
vertical-align: top;
}
th {
background: #00B0F0;
}
tr+tr th,
tbody th {
background: #DAEEF3;
}
tr+tr,
tbody {
text-align: left
}
table,
th,
td {
border: solid 1px;
border-collapse: collapse;
table-layout: fixed;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id='userdata'>
<tr>
<th colspan="2" id="ct">CT INFO</th>
<th colspan="4" id="que">Question</th>
<th colspan="4" id="user">User Info</th>
</tr>
<tr>
<th>CT ID</th>
<th>CT</th>
<th>Id</th>
<th>isActive</th>
<th>is Complex</th>
<th>is Breakdown</th>
<th>ID</th>
<th>NAME</th>
<th>Data</th>
<th>updatedAt</th>
</tr>
<tbody>
</tbody>
</table>

[<>]and created you a minimal reproducible example