Untitled
unknown
javascript
4 years ago
1.9 kB
5
Indexable
const fileSelector = document.getElementById("fileInput"); fileSelector.addEventListener("change", (event) => { const fileList = event.target.files; danger.hidden = true; danger.innerHTML = ""; showFiles(fileList); }); let fileObject = {}; const danger = document.getElementById("danger"); function showFiles(fileList) { fileObject = {}; const output = document.getElementById("output"); let counter = 0; output.innerHTML = ` <div class="spinner-border" role="status"> <span class="visually-hidden">Loading...</span> </div>`; readFiles(fileList); console.log(fileObject); setTimeout(() => { output.innerHTML = `<tr> <th>№</th> <th>Номенклатура</th> <th>Количество</th> <th>Ед</th> </tr>`; for (const key in fileObject) { counter++; output.innerHTML += `<tr> <td>${counter}</td> <td>${key}</td> <td>${fileObject[key]}</td> <td>шт</td> </tr>`; } }, 1000); } function readFiles(files) { for (const file of files) { languageEncoding(file).then((fileInfo) => { if (fileInfo.encoding !== "UTF-8") { danger.hidden = false; danger.innerHTML += file.name + " - have wrong encoding <br/>"; } }); const reader = new FileReader(); reader.readAsText(file); reader.onload = function () { fileInsertToObject(reader.result); }; reader.onerror = function () { console.log(reader.error); }; } } function fileInsertToObject(file) { let formattedFile = file.split("\n"); formattedFile.shift(); for (const line of formattedFile) { const splitLine = line.split("\t"); if (!splitLine[2]) continue; const amount = splitLine[2].replace(/,/gi, "."); if (fileObject.hasOwnProperty(splitLine[1])) { fileObject[splitLine[1]] += +amount; } else { fileObject[splitLine[1]] = +amount; } } }
Editor is loading...