我的目标是根据用户在下拉菜单中的 Select 来过滤数据.
SET UP个
我有一个名为employees
的对象array.首先,我按salary
属性排序,然后使用另一个函数manipulateData()
,我按role
属性(用户通过下拉菜单 Select )进行筛选.
一百零二
以下是我的代码:
JavaScript:个
let employees = [
{
id : 1,
name : 'John Smith',
role : 'Sales',
salary : 100000
},
{
id : 2,
name : 'Mary Jones',
role : 'Finance',
salary : 78000,
},
{
id : 3,
name : 'Philip Lee',
role : 'Finance',
salary : 160000,
},
{
id : 4,
name : 'Susan Williams',
role : 'Sales',
salary : 225000,
},
{
id : 5,
name : 'Jason Alexander',
role : 'Finance',
salary : 90000,
},
{
id : 6,
name : 'Derek Sanders',
role : 'Sales',
salary : 140000,
}
]
//#
function filterData() {
let allData = employees;
let selectBox = document.getElementById("employee-role");
let selectedValue = selectBox.options[selectBox.selectedIndex].value;
// SORT SALARIES DESCENDING
allData.sort(function (a, b) {
return b.salary - a.salary;
})
console.log('Sorted Data', allData);
// *** Here is where I'd like to invoke the dataManipulation() function to filter on only the user-selected roles
// LIMIT TO ONLY 2 RECORDS
let filteredData = allData.filter( (value, index) => {
return (index < 2);
});
// RENDER THE CHART
//renderChart(filteredData);
}
function manipulateData(filteredData) {
return filteredData.filter(e => e.role === selectedValue)
}
HTML:个
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="description" content="">
<title>Example</title>
</head>
<body>
<div class="container-fluid">
<!-- header -->
<div class="row justify-content-center">
<div class="align-self-center">
<h1>Example</h1>
</div>
</div>
<!-- drop down menu -->
<div class="row justify-content-center">
<div class="align-self-center">
<div class="user-control">
<div class="form-group">
<select id="employee-role" class="form-control" onchange="manipulateData()">
<option value="All">All Roles</option>
<option value="Sales">Sales</option>
<option value="Finance">Finance</option>
</select>
</div>
</div>
</div>
</div>
<!-- container of bar chart -->
<div id="chart-area"></div>
</div>
</body>
</html>
Question:在我将数据集的大小减少到2条记录之前,如何合并manipulateData()
函数以仅获取用户通过下拉菜单 Select 的role
条记录?
谢谢!(这里是JavaScript新手)