支付页面显示的优惠券默认为最大优惠券

164 阅读1分钟

刚开始没有考虑到id 所以只找到最大价格的钱数 遍历之后 存到数组里面 显示在页面上最大的 newMax[max.length-1] 获取最大值 newMax[0] // 获取最小值: ``


     res.data.data.forEach((element) => {
            max.push(element.couponMoney) 
          })
        const newMax = max.sort((a, b) => a - b) 
        this.couponMoney = newMax[max.length-1]

后面需要获取id所以把接口里面获得的数据数组进行价格排序 把最大的价格始终显示在第一个


getCoupon(){
	var that = this;
	this.model.status =1
        request.httpRequest('payment/couponUser',this.model).then((res)=>{
            let max = []
	if(res.data.code==200){
	    var maxArr = res.data.data
            // res.data.data.forEach((element) => {
		// max.push(element.couponMoney) })
               //  const newMax = max.sort((a, b) => a - b) 
              //   this.couponMoney = newMax[max.length-1]
             //  给数组进行按照从大到小的排序 然后只把最大的价格显示在页面上 
              var compare = function (prop) {
		 return function (obj1, obj2) {
		 var val1 = obj1[prop];
		 var val2 = obj2[prop];
                 if (val1 < val2) {
			 return 1;
		   } else if (val1 > val2) {
			 return -1;
		   } else {
			 return 0;
			  }            
		     } 
		}
              maxArr.sort(compare("couponMoney")) 
	      this.couponMoney = maxArr[0].couponMoney
	      this.cardId = maxArr[0].couponId
						
	}
},