<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<button onclick="fazerRequisicao();">Pesquisar</button>
<input type="text" id="hostgroup" style="width: 100px;" value="Portal">
<hr/>
<div id="div-responsetext"></div>
<table id="producttable">
<thead>
<tr>
<th>Group</th>
<th>License</th>
</tr>
</thead>
<tbody>
<!-- existing data could optionally be included here -->
</tbody>
</table>
<template id="productrow">
<tr>
<td class="group"></td>
<td class="license"></td>
</tr>
</template>
</body>
</html>
var urlapi = "http://xxxxx.xxxx";
// var len = myJSON.hostGroup.name.length, aryHostGroup = [];
function fazerRequisicao(){
var url = urlapi + document.getElementById("hostgroup").value;
var xhttp = new XMLHttpRequest();
xhttp.open("GET", url, false);
xhttp.send();
var obj = JSON.parse(xhttp.responseText);
var data_map = new Map();
var index = 0;
for (i = 0; i < obj.length; i++) {
if(data_map.has(obj[i].hostGroup.name)) {
data_map.set(obj[i].hostGroup.name, data_map.get(obj[i].hostGroup.name) + obj[i].consumedHostUnits);
} else {
data_map.set(obj[i].hostGroup.name, +obj[i].consumedHostUnits);
}
}
const objConverted = Object.fromEntries(data_map);
var myJSON = JSON.stringify(objConverted);
document.getElementById("div-responsetext").innerHTML = myJSON;
}
// Build table dinamically
function buildTable() {
let groups = mapObject(getMockResult());
// Instancie a tabela com o HTML tbody e a row com o template
var t = document.querySelector('#productrow');
var td = t.content.querySelectorAll("td");
for(var i = 0; i < groups.length; i++) {
td[0].textContent = groups[i].name;
td[1].textContent = groups[i].license;
var tb = document.getElementsByTagName("tbody");
var clone = document.importNode(t.content, true);
tb[0].appendChild(clone);
}
}
function mapObject(data) {
return Object.entries(data).map(([name, license]) => ({name, license}));
}
function getMockResult() {
return {
"GFUnificado":14,
"APIGateway":22,
"OSBSegregado1":202,
"Portal":14,
"OAM":30,
"MicroServicos":68,
"Loja":58.5,
"60-Lojas":46,
"-Callcenter":37,
"360-Dealers":24,
"SOAMecsol":30,
"MeuEmpresas":40,
"OSBFarm4":89,
"Prisma":8,
"EricssonRevenueManager":50.75,
"N":80,
"Atl":10,
"OSBEAI":2,
"Next":208.5
};
}
buildTable();
Output
This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account
Dismiss xKeyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |