循环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)
          }

        }
      }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值