×

vue每日一学:温故forEach()在实战项目的基础知识和应用

作者:天空2021.07.01来源:Web前端之家浏览:4177评论:0
关键词:vuejsforEach()

今天是2021年7月1日,中国共产党100岁生日,从1921到2021,从石库门到天安门,今天是你的生日,亲爱的中国共产党,百年历程波澜壮阔,百年大党风华正茂!

回到今日主题:vue每日一学:温故forEach()的应用知识。

forEach() 是前端开发中操作数组的一种方法,主要功能是遍历数组,其实就是 for 循环的升级版,该语句需要有一个回调函数作为参数。回调函数的形参依次为:

  • value:遍历数组的内容

  • index:对应数组的索引

  • array:数组自身

Vue 项目中,标签里的循环使用 v-for,方法里面的循环使用 forEach。

forEach() 使用原理

forEach() 方法主要是用于调用数组的每个元素,并将元素传递给回调函数。需要注意的是: forEach() 方法对于空数组是不会执行回调函数的。

forEach:即 Array.prototype.forEach,只有数组才有的方法,相当于 for 循环遍历数组。用法:arr.forEach(function(item,index,array){...}),其中回调函数有 3 个参数,item 为当前遍历到的元素,index 为当前遍历到的元素下标,array 为数组本身。forEach 方法不会跳过 null 和 undefined 元素。比如数组[1,undefine,null,,2]中的四个元素都将被遍历到,注意与 map 的区别。

forEach() 语法

array.forEach(
    function(currentValue, index, array), thisValue
)

eg:

array.forEach(
    function(item,index,array){
     ... 
    }
)

内容拓展

forEach()的 continue 和 break:

forEach() 自身不支持 continue 和 break 语句的,但是可以通过 some 和 every 来实现。

forEach()与 map 的区别:

forEach()没有返回值,性质上等同于 for 循环,对每一项都执行 function 函数。即 map 是返回一个新数组,原数组不变,而 forEach 是改变原数组。

forEach()与 for 循环的对比:

for 循环步骤多比较复杂,forEach 循环比较简单好用,不易出错。

forEach()例子:

eg1:

let array = [1, 2, 3, 4, 5, 6, 7];
array.forEach(function (item, index) {
    console.log(item); //输出数组的每一个元素
});

eg2:

var array=[1, 2, 3, 4, 5];
array.forEach(function(item, index, array){
    array[index]=4 * item;
});
console.log(array);    //输出结果:修改了原数组元素,为每个元素都乘以4

eg3:

 <el-checkbox v-for="(item) in searchContent"
                       :label="item.id"
                       :key="item.id"
                       class="checkbox">
            <span>{{item.value}}{{item.checked}}</span>
          </el-checkbox>
  handle(index, row) {
        this.selectedCheck=[];
        let a = this;
        this.jurisdiction = true;
        this.roleId = row.id;
        this.$http.get(“/user/resources", {
            params: {userId: this.userId}
          }).then((response) => {
          a.searchContent = response.body;
          a.searchContent.forEach(function (b) {
            if(b[‘checked']){
              a.selectedCheck.push(b.id);
            }
          })
   })

eg4:

var userList = new Array();
        var data = {};
        if (response.data.userList != null && response.data.userList.length > 0) {
          response.data.userList.forEach((item, index) => {

            data.a = item.a;
            data.b = item.b;
            data.arr1 = new Array();
            data.arr1[0] = item.c;
            data.arr1[1] = item.d;
            data.e = item.e;
            data.f = item.f;
            data.arr2 = new Array();
            data.arr2[0] = item.j;
            data.arr2[1] = item.h;
            userList.push(data);
          });
        }

eg5:

searchDept(keyWord, callback) {
       if (keyWord) {
        this.$service.data
          .searchDepts({ data: { full_name: keyWord } })
          .then(r => {
            if (r.Success) {
              let arr = [];
              r.Data.Result.forEach(element => {
                arr.push({
                  id: element.work_id,
                  value: element.full_name,
                  dept: element
                });
              });
              callback(arr);
            }
         });
      }
    },

OK,就分享这么多了,大家可以去实际操作下吧,有问题可以加群或者留言哦。

您的支持是我们创作的动力!
温馨提示:本文作者系 ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://www.jiangweishan.com/article/vuejs20210701d3.html

网友评论文明上网理性发言 已有0人参与

发表评论: