filter()方法的使用

一:作用

filter用于对数组进行过滤。
它创建一个新数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。

 

二:语法

Array.filter(function(currentValue, indedx, arr), thisValue)

其中,函数 function 为必须,数组中的每个元素都会执行这个函数。且如果返回值为 true,则该元素被保留;
函数的第一个参数 currentValue 也为必须,代表当前元素的值。

 

三:示例

返回数组nums中所有大于5的元素。

let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

let res = nums.filter((num) => {
  return num > 5;
});

console.log(res);  // [6, 7, 8, 9, 10]

四: 在实际Element ui 项目中应用的实例

根据上面的下拉选择选择不同的payment方式进行展示,后台数据返回的右侧示例

selectPayment 是el-select的v-modal选中的值,通过computed计算完之后,使用showList展示数据,当下拉列表选择的时候,就会展示相应的数据了。

computed: {

  showList() {

    let list = JsonCoyp(this.list)

    list = list.filter(item => this.selectPayment === '' "  || item.code === this.selectPaymeng)

    return list

  }

}

 

posted @ 2021-07-15 14:25  慕雪琳鸢  阅读(2975)  评论(0)    收藏  举报