我有一个二维字符串数组,例如:
[["Application1", "11106.exampleserver.com", "11109.exampleserver.com", "11102.exampleserver.com", "11105.exampleserver.com" "Database, AFPUOR(KNAJKLD)", "Database, UOQZRNJ(LKUJD)" ],
["Application2", "44407.exampleserver.com", "11106.exampleserver.com", "11104.exampleserver.com", "Database, POJPR (OIUOLWA) ", "Database, UIAHSD (JJJQEP)" ],...]
依此类推..(每次服务器/数据库数量不同)
如何按数据库/服务器对“应用程序”排序并显示/保存这些数据库/服务器? 管理阵列的最佳方法是什么?
我需要一个HTML表:在表头中,应用程序的名称,然后是服务器和数据库的名称(我需要划分服务器和数据库)。
现在,我可以使用以下代码输出数据库:
for(j=0; j < columns.length; j++){
for(i=0;i < columns[j].length; i++){
var db = columns[j][i].match("Database")
if (db != null){
console.log("APP: " + j + ": " + columns[j][0] + " , ID: " + i + ": " + db.input)
//outpus e.g.: APP: 0: Application1, ID: 5: Database XYZ
}
}
}
和具有以下代码的服务器:
for(j=0; j < columns.length; j++){
for(i=1;i < columns[j].length; i++){
var db = columns[j][i].match("Database")
if (db == null){
console.log("APP: " + j + ": " + columns[j][0] + " , ID: " + i + ": " + columns[j][i])
//outpus e.g.: APP: 0: Application1, ID: 1: Server1.1
}
}
}
答案 0 :(得分:0)
尝试一下,我尝试按应用程序然后按类型对数据进行分组:
var d = [["Application1", "11106.exampleserver.com", "11109.exampleserver.com", "11102.exampleserver.com", "11105.exampleserver.com", "Database, AFPUOR(KNAJKLD)", "Database, UOQZRNJ(LKUJD)"], ["Application2", "44407.exampleserver.com", "11106.exampleserver.com", "11104.exampleserver.com", "Database, POJPR (OIUOLWA) ", "Database, UIAHSD (JJJQEP)"]];
var data = {};
for (var i = 0; i < d.length; i++) {
data[d[i][0]] = {
Server: [],
Database: []
};
console.log(d[i][0]);
for (var j = 1; i < d[i].length; j++) {
console.log(d[i][j]);
if (typeof d[i][j] == 'undefined')
break;
if (d[i][j].match('Database')) {
data[d[i][0]]['Database'].push(d[i][j]);
} else {
data[d[i][0]]['Server'].push(d[i][j]);
}
}
}
document.write(JSON.stringify(data));
输出:
{
"Application1": {
"Server": [
"11106.exampleserver.com",
"11109.exampleserver.com",
"11102.exampleserver.com",
"11105.exampleserver.com"
],
"Database": [
"Database, AFPUOR(KNAJKLD)",
"Database, UOQZRNJ(LKUJD)"
]
},
"Application2": {
"Server": [
"44407.exampleserver.com",
"11106.exampleserver.com",
"11104.exampleserver.com"
],
"Database": [
"Database, POJPR (OIUOLWA) ",
"Database, UIAHSD (JJJQEP)"
]
}
}
答案 1 :(得分:0)
尝试一下,
在您的html中
<table >
<thead>
<tr>
<th>Application</th>
<th>ID</th>
<th>Database</th>
<th>Server</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
在您的JavaScript中
const table = document.getElementsByTagName('tbody')[0];
data.forEach((entity,index) => {
let rowData = {}
rowData.applicationName = entity[0]
rowData.id = index
rowData.databases = []
rowData.servers = []
entity.forEach(value => {
if (value.match('Database')){
rowData.databases.push(value)
}else{
rowData.servers.push(value)
}
})
appendRow(rowData)
})
function appendRow(rowData) {
table.innerHTML += `<tr>
<td>${rowData.applicationName}</td>
<td>${rowData.id}</td>
<td>${rowData.databases.join(',')}</td>
<td>${rowData.servers.join(',')}</td>
</tr>`
}
希望这会有所帮助!