Binary way to save multiple selection results

// 保存
          var switchList = 0;
          if(that.dingdan){
            switchList += Math.pow(2,0);
          };
          if(that.vipCard){
            switchList += Math.pow(2,1);
          };
          if(that.coupon){
            switchList += Math.pow(2,2);
          };
          if(that.chuzhi){
            switchList += Math.pow(2,3);
          };
          if(that.score){
            switchList += Math.pow(2,4);
          };
          if(that.address){
            switchList += Math.pow(2,5);
          };
          if(that.shopCar){
            switchList += Math.pow(2,6);
          };
          if(that.indexPage){
            switchList += Math.pow(2,7);
          };
          console.log(switchList);
// 取值                
                if(data && data.ret == 0){
                var userCenterCustom = data.model.userCenterCustom;
                if((userCenterCustom & (Math.pow(2,0))) != 0){
                  vm.dingdan = true;
                } else {
                  vm.dingdan = false;
                };
                if((userCenterCustom & (Math.pow(2,1))) != 0){
                  vm.vipCard = true;
                } else {
                  vm.vipCard = false;
                };
                if((userCenterCustom & (Math.pow(2,2))) != 0){
                  vm.coupon = true;
                } else {
                  vm.coupon = false;
                };
                if((userCenterCustom & (Math.pow(2,3))) != 0){
                  vm.chuzhi = true;
                } else {
                  vm.chuzhi = false;
                };
                if((userCenterCustom & (Math.pow(2,4))) != 0){
                  vm.score = true;
                } else {
                  vm.score = false;
                };
                if((userCenterCustom & (Math.pow(2,5))) != 0){
                  vm.address = true;
                } else {
                  vm.address = false;
                };
                if((userCenterCustom & (Math.pow(2,6))) != 0){
                  vm.shopCar = true;
                } else {
                  vm.shopCar = false;
                };
                if((userCenterCustom & (Math.pow(2,7))) != 0){
                  vm.indexPage = true;
                } else {
                  vm.indexPage = false;
                };
              }

Figure 1 switchList has the same value as Figure 2 userCenterCustom

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325037397&siteId=291194637
Recommended