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