最近做表格数据的查询过滤功能,为防止每次查询数据都要进行网络请求,所以我使用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})
}