If I've got the layout of the data structure right, it is a key/value object, which in turn contains objects that have information such as connected.
In that case you can use Object.keys
to get the keys first, sort those Object.keys(data).sort()
, and create the html from there. For example:
function showClients(data) {
var html = Object.keys(data).sort().reduce(function(h, clientId){
return h + '<p>' + clientId
+ (data[clientId].connected ? ' connected' : ' disconnected')
+ '</p>';
}, '')
|| '<p>No clients connected</p>';
$('#connected_clients').html(html);
}
function showClients(data) {
var html = Object.keys(data).sort().reduce(function(h, clientId){
return h + '<p>' + clientId
+ (data[clientId].connected ? ' connected' : ' disconnected')
+ '</p>';
}, '')
|| '<p>No clients connected</p>';
$('#connected_clients').html(html);
}
showClients({Client3:{connected:true},Client1:{connected:true},Client2:{connected:false}});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id=connected_clients></div>