最近做表格數據的查詢過濾功能,為防止每次查詢數據都要進行網絡請求,所以我使用js的內置方法filter來進行數據的過濾,以達到數據查詢的效果。
一、filter方法介紹
語法:array.filter(function(currentValue,index,arr), thisValue)
參數 | 用處 |
---|---|
function(currentValue,index,arr) | 每個元素都回調用該方法 |
currentValue | 必須。當前元素的值 |
index | 可選。當前元素的索引值 |
arr 可選。 | 當前元素屬于的數組對象 |
thisValue. | 可選。對象作為該執行回調時使用,傳遞給函數,用作 "this" 的值。如果省略了 thisValue ,"this" 的值為 "undefined" |
//SecondData是數據副本,Data是要展示的值,selectname是想要過濾的條件。使用該方法從副本中獲取想要的數據。
Filter:function(){
Data = SecondData.filter(e => { return e.name == selectname})
}