<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>数据筛选与排序工具</title>
<style>
body {
font-family: Arial, sans-serif;
background: linear-gradient(45deg, #3498db, #8e44ad);
margin: 0;
padding: 20px;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.container {
background-color: rgba(255, 255, 255, 0.9);
border-radius: 10px;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 80%;
max-width: 800px;
}
h1 {
text-align: center;
color: #2c3e50;
}
textarea {
width: 100%;
height: 150px;
margin-bottom: 10px;
padding: 10px;
border: 1px solid #bdc3c7;
border-radius: 5px;
resize: vertical;
}
.controls {
display: flex;
justify-content: space-between;
margin-bottom: 10px;
}
select, button {
padding: 10px;
border: none;
border-radius: 5px;
background-color: #3498db;
color: white;
cursor: pointer;
transition: background-color 0.3s;
}
select:hover, button:hover {
background-color: #2980b9;
}
#output {
background-color: #ecf0f1;
border-radius: 5px;
padding: 10px;
white-space: pre-wrap;
}
</style>
</head>
<body>
<div class="container">
<h1>数据筛选与排序工具</h1>
<textarea id="input" placeholder="请输入JSON格式的数据,每个对象一行"></textarea>
<div class="controls">
<select id="filterKey">
<option value="">选择筛选键</option>
</select>
<input type="text" id="filterValue" placeholder="筛选值">
<select id="sortKey">
<option value="">选择排序键</option>
</select>
<select id="sortOrder">
<option value="asc">升序</option>
<option value="desc">降序</option>
</select>
<button onclick="processData()">处理数据</button>
</div>
<div id="output"></div>
</div>
<script>
let data = [];
function processData() {
const input = document.getElementById('input').value;
data = input.split('\n').map(line => {
try {
return JSON.parse(line);
} catch (e) {
return null;
}
}).filter(item => item !== null);
updateSelectOptions();
filterAndSortData();
}
function updateSelectOptions() {
const keys = Object.keys(data[0] || {});
const filterKey = document.getElementById('filterKey');
const sortKey = document.getElementById('sortKey');
filterKey.innerHTML = '<option value="">选择筛选键</option>';
sortKey.innerHTML = '<option value="">选择排序键</option>';
keys.forEach(key => {
filterKey.innerHTML += `<option value="${key}">${key}</option>`;
sortKey.innerHTML += `<option value="${key}">${key}</option>`;
});
}
function filterAndSortData() {
const filterKey = document.getElementById('filterKey').value;
const filterValue = document.getElementById('filterValue').value;
const sortKey = document.getElementById('sortKey').value;
const sortOrder = document.getElementById('sortOrder').value;
let result = data;
if (filterKey && filterValue) {
result = result.filter(item => String(item[filterKey]).includes(filterValue));
}
if (sortKey) {
result.sort((a, b) => {
if (a[sortKey] < b[sortKey]) return sortOrder === 'asc' ? -1 : 1;
if (a[sortKey] > b[sortKey]) return sortOrder === 'asc' ? 1 : -1;
return 0;
});
}
document.getElementById('output').textContent = JSON.stringify(result, null, 2);
}
</script>
</body>
</html>