javascript - 如何在 JavaScript 中选择不重复的项目
问题描述
我有一个网站脚本,它将读取用户上传的 csv 文件并将其显示在 html 表上。
当用户上传他们的 csv 文件时,我的脚本将自动添加一个名为的新列并将Device
列表项分发到现有的 csv 记录。
例如,如果用户上传的 csv 包含 4 条记录,则该Device
列将根据此列表添加三个序列号:
var filterserial = ['K4Z7-9RFL-XCXS', 'TEIK-YMJF-4QQG', '4SB9-NR2D-742E', 'TY83-FPSX-C3WS', 'YVLM-W2T1-YNSI']
例子:
问题:当多次尝试上传 csv 文件时,设备序列号可能会重复。由于每个 ID 的所有设备序列号都应该是唯一的,我怎样才能防止设备序列号被重复?提前致谢 例如:
完整代码(HTML 和 JAVSCRIPT):
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="https://unpkg.com/xlsx@0.15.1/dist/xlsx.full.min.js"></script>
</head>
<body>
<div class="container">
<h2 class="text-center mt-4 mb-4">Convert Excel to HTML Table using JavaScript</h2>
<div class="card">
<div class="card-header"><b>Select Excel File</b></div>
<div class="card-body">
<input type="file" id="excel_file" />
</div>
</div>
<div id="excel_data" class="mt-5"></div>
</div>
</body>
</html>
<script>
const excel_file = document.getElementById('excel_file');
function randomNoRepeats(array) {
var copy = array.slice(0);
return function() {
if (copy.length < 1) { copy = array.slice(0); }
var index = Math.floor(Math.random() * copy.length);
var item = copy[index];
copy.splice(index, 1);
return item;
};
}
var filterserial = ['K4Z7-9RFL-XCXS', 'TEIK-YMJF-4QQG', '4SB9-NR2D-742E', 'TY83-FPSX-C3WS', 'YVLM-W2T1-YNSI']
var chooser = randomNoRepeats(filterserial);
excel_file.addEventListener('change', (event) => {
if (!['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel'].includes(event.target.files[0].type)) {
document.getElementById('excel_data').innerHTML = '<div class="alert alert-danger">Only .xlsx or .xls file format are allowed</div>';
excel_file.value = '';
return false;
}
var reader = new FileReader();
reader.readAsArrayBuffer(event.target.files[0]);
reader.onload = function(event) {
var data = new Uint8Array(reader.result);
var work_book = XLSX.read(data, {
type: 'array'
});
var sheet_name = work_book.SheetNames;
var sheet_data = XLSX.utils.sheet_to_json(work_book.Sheets[sheet_name[0]], {
header: 1
});
if (sheet_data.length > 0) {
var table_output = '<table class="table table-striped table-bordered">';
for (var row = 0; row < sheet_data.length; row++) {
table_output += '<tr>';
if (row == 0) {
table_output += '<th>' + ['Device'] + '</th>'
} else {
table_output += '<td>' + [chooser()] + '</td>'
}
for (var cell = 0; cell < sheet_data[row].length; cell++) {
if (row == 0) {
table_output += '<th>' + sheet_data[row][cell] + '</th>';
} else {
table_output += '<td>' + sheet_data[row][cell] + '</td>';
}
}
table_output += '</tr>';
}
table_output += '</table>';
document.getElementById('excel_data').innerHTML = table_output;
}
excel_file.value = '';
}
});
</script>
解决方案
如何在更改侦听器中移动选择器初始化并以这种方式定义它?
var shuffledSerials = filterserial.slice(0).sort(() => return 0.5 - Math.random() );
var chooser = () => shuffledSerials.pop();
这样,每次加载新文件时连续序列都会被打乱,选择器只会从新创建的数组中弹出项目。
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="https://unpkg.com/xlsx@0.15.1/dist/xlsx.full.min.js"></script>
</head>
<body>
<div class="container">
<h2 class="text-center mt-4 mb-4">Convert Excel to HTML Table using JavaScript</h2>
<div class="card">
<div class="card-header"><b>Select Excel File</b></div>
<div class="card-body">
<input type="file" id="excel_file" />
</div>
</div>
<div id="excel_data" class="mt-5"></div>
</div>
</body>
</html>
<script>
const excel_file = document.getElementById('excel_file');
var filterserial = ['K4Z7-9RFL-XCXS', 'TEIK-YMJF-4QQG', '4SB9-NR2D-742E', 'TY83-FPSX-C3WS', 'YVLM-W2T1-YNSI'];
excel_file.addEventListener('change', (event) => {
if (!['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel'].includes(event.target.files[0].type)) {
document.getElementById('excel_data').innerHTML = '<div class="alert alert-danger">Only .xlsx or .xls file format are allowed</div>';
excel_file.value = '';
return false;
}
var shuffledSerials = filterserial.slice(0).sort(() => 0.5 - Math.random() );
var chooser = () => shuffledSerials.pop();
var reader = new FileReader();
reader.readAsArrayBuffer(event.target.files[0]);
reader.onload = function(event) {
var data = new Uint8Array(reader.result);
var work_book = XLSX.read(data, {
type: 'array'
});
var sheet_name = work_book.SheetNames;
var sheet_data = XLSX.utils.sheet_to_json(work_book.Sheets[sheet_name[0]], {
header: 1
});
if (sheet_data.length > 0) {
var table_output = '<table class="table table-striped table-bordered">';
for (var row = 0; row < sheet_data.length; row++) {
table_output += '<tr>';
if (row == 0) {
table_output += '<th>' + ['Device'] + '</th>'
} else {
table_output += '<td>' + [chooser()] + '</td>'
}
for (var cell = 0; cell < sheet_data[row].length; cell++) {
if (row == 0) {
table_output += '<th>' + sheet_data[row][cell] + '</th>';
} else {
table_output += '<td>' + sheet_data[row][cell] + '</td>';
}
}
table_output += '</tr>';
}
table_output += '</table>';
document.getElementById('excel_data').innerHTML = table_output;
}
excel_file.value = '';
}
});
</script>
推荐阅读
- reactjs - 尝试在 React 中迭代 URL 数组?
- heroku - Heroku 部署 - 未在 requirements.txt 上找到 javabridge 包的 java_home
- c# - 用于检索包含列表中任何值的项目的 LINQ 查询?
- xcode - .onAppear() 中的多个语句不起作用
- jenkins - Jenkinsfile 全局共享变量
- css - SCSS/Sass:使用来自一个 scss 类的计算变量作为另一个类的基础
- git - 维护 3 个存储库的适当命令链
- ios - React Native 应用程序在发布模式下部署时使我的 iPhone 崩溃
- javascript - 如果我使用 JavaScript 更改加载时的 ID,Bootstrap Collapse 不起作用
- java - 如何仅显示数组中的可用元素?