【发布时间】:2015-10-09 07:49:12
【问题描述】:
我制作的这个函数可以用于任何二维数组:
GenerateTableByArray: function(tableData){
var body = document.getElementsByTagName("body")[0];
var table = document.createElement('table');
var tableBody = document.createElement('tbody');
tableData.forEach(function(rowData) {
//creating rows for the table
var row = document.createElement('tr');
row.style.backgroundColor = "#EEF4EA";
//creating cell for every row
rowData.forEach(function(cellData) {
var cell = document.createElement('td');
cell.appendChild(document.createTextNode(cellData));
row.appendChild(cell);
});
tableBody.appendChild(row);
});
//appending the generated table into the html body (with border as attribute)
table.appendChild(tableBody);
table.setAttribute("border", "1");
document.body.appendChild(table);
}
这个函数适用于我传递的任何二维数组。
我遇到的问题是用我的 json 对象中的特定值填充数组。
这里是对象:
var DashboardJSON = {
"data": [
{
"name": "Person1",
"date": "2010-10-08",
"pct": 81.25
},
{
"name": "Person1",
"date": "2010-10-09",
"pct": 56.25
},
{
"name": "Person2",
"date": "2010-09-11",
"pct": 82.22
}
]
}
我需要让它看起来像这样:
|2010-10-08|2010-10-09|2010-09-11
------------------------------------------
Person1 | 81.25 | 56.25 |
Person2 | | | 82.22
这是我创建二维数组的函数:
Main: function () {
//First i get the unique names
var uniqueNames = {};
var distinctNames = [];
for( var i in DashboardJSON.data ){
if( typeof(uniqueNames[DashboardJSON.data[i].name]) == "undefined"){
distinctNames.push(DashboardJSON.data[i].name);
}
uniqueNames[DashboardJSON.data[i].name] = 0;
}
//Then the unique dates
var uniqueDates = {};
var distinctDates = [];
for( var i in DashboardJSON.data ){
if( typeof(uniqueDates[DashboardJSON.data[i].date]) == "undefined"){
distinctDates.push(DashboardJSON.data[i].date);
}
uniqueDates[DashboardJSON.data[i].date] = 0;
}
var ROWS = distinctNames.length+1;
var COLS = distinctDates.length+1;
//creating two-dimensional array
var my2dArray = new Array(ROWS);
for (var i = 0; i < my2dArray.length; i++) {
my2dArray[i] = new Array(COLS);
}
//filling the array with data
for(key in DashboardJSON.data){
for(var i = 0; i < ROWS; i++){
for(var j = 0; j < COLS; j++){
//first cell is empty
if(i==0 && j==0){
my2dArray[i][j]="";
}
//the column titles are filled from the dates array
if(i==0 && j>0){
my2dArray[i][j+1]=distinctDates[j-1];
}
//the row titles are filled with the names array
if(j==0 && i>0 ){
my2dArray[i][j]=distinctNames[i-1];
//HERE i need to populate the array with the point for each person by date but it doesn't work
} else if (i!=0 && j!=0){
if(DashboardJSON.data[key].name == distinctNames[i-1]){
my2dArray[i][j]=DashboardJSON.data[key].pct;
console.log(my2dArray[i][j]);
}
}
}
}
}
//calling the function to generate my table
GenerateTableByArray(my2dArray);
}
由于某种原因,它无法正常工作,并且我的表格中只填满了人的最后一点,而不是全部。 :/
我只需要用 javascript 来解决这个问题。
【问题讨论】:
-
我认为如果:“if(DashboardJSON.data[key].name == distinctNames[i-1])”,您还需要在其中添加日期检查。如果不是,您将把这些人的最后一个点放到所有列中,因为您没有检查该值是否与列的日期匹配。
-
谢谢,伙计,确实是这样!我不知道我一开始怎么没想到。 :) 事实证明,我的生成表格的函数不适用于备用矩阵,所以我必须先用空字符串填充我的数组。现在它工作完美。 :)如果你愿意 - 把它写成答案,这样我就可以投票了。
-
@Nyagolova 非常感谢。你的代码对我有用。
标签: javascript json multidimensional-array