안녕하세요 for문에 대해 헷갈려 질문 드립니다
현재 json 데이터를 활용한 테이블을 구현하고 있습니다.
let monthPrint = [
{ SelectedDate: '2022-12-01' },
{ SelectedDate: '2022-12-02' },
/* ... */
{ SelectedDate: '2022-12-31' }
];
let itemCountList = [
{ Itemdate: '2022-12-09', Itemcounta: '4', Itemcountc: '0' },
{ Itemdate: '2022-12-19', Itemcounta: '0', Itemcountc: '5' },
{ Itemdate: '2022-12-20', Itemcounta: '0', Itemcountc: '5' },
{ Itemdate: '2022-12-21', Itemcounta: '0', Itemcountc: '5' },
{ Itemdate: '2022-12-22', Itemcounta: '0', Itemcountc: '5' },
{ Itemdate: '2022-12-23', Itemcounta: '0', Itemcountc: '4' },
{ Itemdate: '2022-12-26', Itemcounta: '0', Itemcountc: '5' }
];
let items = ['Itemcounta', 'Itemcountc'];
date | itemcounta | itemcountc |
---|---|---|
12-01 | - | - |
12-02 | 0 | 5 |
12-03 | 0 | 6 |
12-04 | - | - |
12-05 | - | - |
12-06 | - | - |
이런식으로 monthPrint.SelectedDate와 itemCountList.Itemdate 날짜가 같다면
itemcounta 와 itemcountc를 items의 갯수 (2개)만큼 나열하고싶습니다. 날짜가 다르다면 '-' 을 붙이고 싶습니다.
for (var i = 0; i < monthPrint.length; i++) {
ExcelTable += '<tr><td>' + monthPrint[i].SelectedDate.substr(5, 10) + '</td>';
for (var j = 0; j < itemCountList.length; j++) {
if (monthPrint[i].SelectedDate.substr(5, 10) == itemCountList[j].Itemdate.substr(5, 10)) {
for (var k = 0; k < items.length; k++) {
ExcelTable += '<td>' + itemCountList[j][items[k]] + '</td>';
}
ExcelTable += '</tr>';
} else {
}
}
}
else 부분에서 continue를 하더라도 itemCountList에 들어있는 날짜 갯수만큼 비교하여
date | itemcounta | itemcountc | ||||
---|---|---|---|---|---|---|
12-01 | - | - | - | - | - | - |
12-02 | 0 | 5 | - | - | - | - |
12-03 | 0 | 6 | - | - | - | - |
12-04 | - | - | - | - | - | - |
12-05 | - | - | - | - | - | - |
12-06 | - | - | - | - | - | - |
이렇게 6개 다 출력이 됩니다.. 3일동안 붙잡고 했는데도 안되네요 부탁드립니다 ..