基本上这段代码应该生成两个表,一个包含数组中的每个类型,哪个工作正常,另一个包含总和具有相同名称的材料
e.g。假设您输入两个红色油漆(10平方英尺和5平方英尺)的实例和两个蓝色油漆(30平方英尺和10平方英尺)的实例,那么第二个表应显示......
红色油漆:15平方英尺蓝色涂料:40平方英尺
...但是目前它只是将与数组中第一个相同名称的材料加在一起,然后只是吐出每个单独的材料,例如
红漆:15平方英尺蓝色涂料:30平方英尺
蓝色油漆:10平方英尺
我尝试了一些不同的东西,但似乎无法让它正常工作,任何人都有任何想法?
function wallTableFunction(){
var tableDiv = document.getElementById("wallTable"),
table = "<table border='1'>" +
"<thread><th>Room</th><th>Surface</th><th>Material</th><th>Width</th><th>Height</th><th>Surface Area</th></thread>";
var totalDiv = document.getElementById("wallTotals"),
totalTable = "<table border='1'>" +
"<thread><th>Material</th><th>TOTAL</th></thread>";
for(var i = 0, j = walls.length; i<j; i++){
table += walls[i].tableRow();
}
for(var k = 0, l = otherWallArray.length; k<l; k++){
totalTable += otherWallArray[k].totalRow();
}
table += "</table>";
totalTable += "</table>";
tableDiv.innerHTML = table;
totalDiv.innerHTML = totalTable;
}
var wallTotals = function(){
for(var i = 0, j = walls.length; i<j; i++){
var wallMaterialFinal = walls[i].material;
var wallResultTotal = walls[i].result;
var a = new wallThing(wallMaterialFinal, wallResultTotal);
wallThingArray.push(a);
}
otherWallArray[0] = wallThingArray[0];
wallThingArray[0] = null;
};
window.onload = function() {
if(localStorage.getItem("wallArray")!== null){
var storedWalls = JSON.parse(localStorage["wallArray"]);
for(var i = 0, j = storedWalls.length; i<j; i++){
var wallRoom = storedWalls[i].room;
var wallSurf = storedWalls[i].surface;
var wallMat = storedWalls[i].material;
var wallWidth = storedWalls[i].width;
var wallHeight = storedWalls[i].height;
var wallResult = storedWalls[i].result;
var a = new Wall(wallRoom, wallSurf, wallMat, wallWidth, wallHeight, wallResult);
walls.push(a);
}
wallTotals();
for(var q = 0, r = wallThingArray.length; q<r; q++){
if(wallThingArray[q] != null) {
for (var s = 0, t = otherWallArray.length; s<t; s++) {
if(wallThingArray[q] != null) {
if (wallThingArray[q].material == otherWallArray[s].material) {
otherWallArray[s].result += wallThingArray[q].result;
wallThingArray[q] = null;
} else {
otherWallArray.push(wallThingArray[q]);
wallThingArray[q] = null;
}
}
}
}
}
wallTableFunction();
}
};
答案 0 :(得分:0)
当你试图总结时,这里有一个逻辑缺陷:
for(var q = 0, r = wallThingArray.length; q<r; q++){
if(wallThingArray[q] != null) {
for (var s = 0, t = otherWallArray.length; s<t; s++) {
if(wallThingArray[q] != null) {
if (wallThingArray[q].material == otherWallArray[s].material) {
otherWallArray[s].result += wallThingArray[q].result;
wallThingArray[q] = null;
} else {
otherWallArray.push(wallThingArray[q]);
wallThingArray[q] = null;
}
}
}
}
}
最里面的else
表示如果该项与otherWallArray
中的第一项不匹配,则会始终将其推送到otherWallArray
。你想要的只是在你通过每个行循环寻找匹配并且什么也没找到之后才进入otherWallArray
:
for (var q = 0, r = wallThingArray.length; q<r; q++) {
var foundMatch = false;
for (var s = 0, t = otherWallArray.length; s<t; s++) {
if (wallThingArray[q].material == otherWallArray[s].material) {
otherWallArray[s].result += wallThingArray[q].result;
foundMatch = true;
break;
}
}
if (!foundMatch) {
otherWallArray.push(wallThingArray[q]);
wallThingArray[q] = null;
}
}