循环for数据点击一个push一次 里面有的覆盖 没有的添加进去

在return定义cc=[]

  vv(){
        let c = {}
        // 这个数组是每次点击都需要初始化为空的
        let zjy = []
        // 模拟的数据
        var random = new Array(0,1,2,3,4,5,6,7,8,9);
        this.code = Math.floor(Math.random()*10)
        // 当数组为空,直接push
        if(this.cc.length == 0){
          c['id'] = this.code
          this.cc.push(c)
        }else{
          // 当数组不为空,循环this.cc,然后把this.cc的值放入zjy这个数组中
          for(let i=0;i<this.cc.length;i++){
            zjy.push(this.cc[i]['id'])
          }
          console.log('code值:'  + this.code)
          console.log('数组值:' + zjy)
          // 这里拿到zjy这个数组,然后通过includes方法进行判断,判断zjy这个数组中是否包含code这个数据
          // 为true的时候,意味着包含
          if(zjy.includes(this.code) == true){
            console.log(this.code + '已存在')
            // 当存在的时候,通过indexOf获取到存在的下标
            let index = zjy.indexOf(this.code)
            console.log('下标:' + index)
            // 这个时候通过下标把对应的值替换
            // this.cc[index].uid就是你原来的uid值,现在需要把他替换掉
            this.cc[index].uid = this.code  //这里的this.code  是你当前新增的那个uid
            return false;
          }else{
            // else就是为false的时候,直接把code放进this.cc数组中
            c['id'] = this.code
            this.cc.push(c)
          }

        }
      }
发布了33 篇原创文章 · 获赞 0 · 访问量 3182

猜你喜欢

转载自blog.csdn.net/YYY1920/article/details/102948578