2017-08-31 32 views
0

我试图显示一个api中存在的客户端的优惠券。如果一个(或'n')优惠券存在于另一个计数使用过的优惠券的api内,我必须从列表中删除或使用这些优惠券。所使用的优惠券的API响应是这样的:使用拼接和for或forEach

{ 
"State": 200, 
"Response": [ 
    { 
     "IdInvoiceRequest": 104, 
     "Coupons": [ 
      { 
       "IdCoupon": 77236, 
       "Code": "11#E5ZQHZ-GNH" 
      }, 
      { 
       "IdCoupon": 77237, 
       "Code": "12#WM96FY-NGE" 
      }, 
      { 
       "IdCoupon": 77239, 
       "Code": "14#BH92BA-E6N" 
      }, 
      { 
       "IdCoupon": 77240, 
       "Code": "15#FWXNR4-XHP" 
      }, 
      { 
       "IdCoupon": 77241, 
       "Code": "16#7FK5F8-TKM" 
      } 
     ] 
    }, 
    { 
     "IdInvoiceRequest": 143, 
     "Coupons": [ 
      { 
       "IdCoupon": 77238, 
       "Code": "13#BN5MZB-VJ9" 
      } 
     ] 
    } 
], 
"Message": "Informacion correcta", 
"TotalRows": 0, 
"IsCorrect": true} 

问题是当我试图消除使用优惠券。到目前为止我的代码:

function validExist() { 
    vm.getSelected = 
    couponExist.get({ 
     idOrder: vm.idOrder 
    }).$promise.then(function(data) { 

     for (var i = 0; i < data.Response.length; i++) { 

     data.Response[i].Select = vm.exist; 
     console.log(vm.exist); 
     } 

     vm.otherF = vm.coupons 

     for (var i = 0; i < data.Response.length; i++) { 

     data.Response[i].Select = vm.isHere; 
     console.log(vm.isHere); 
     } 

     if (vm.exist == vm.isHere) { 
     vm.coupons.splice(vm.coupons.IdCoupon, i++); 
     }; 

    }); 
} 

当剪接行为,只是消除了第一个优惠券,但其他人仍然是相同的,即使所有的券都用优惠券名单内。我可以做些什么来删除所有的优惠券?我听说有一种方法是使用'forEach'或几个'for',但我没有看到灯光(叹息)。

你能帮我吗?

Thanx提前。

回答

0

Array.prototype.splice需要一个开始索引和要从数组中删除的项目数。我不知道你的代码的其他部分都应该做的,但这里的你如何看的项目从一个阵列和第二阵列中删除一个例子:

var existingCoupons = [ 
 
    { IdCoupon: 111 }, 
 
    { IdCoupon: 222 }, 
 
    { IdCoupon: 333 }, 
 
    { IdCoupon: 444 }, 
 
    { IdCoupon: 555 } 
 
]; 
 
var simulatedResponse = [{ 
 
    IdInvoiceRequest: "abc", 
 
    Coupons: [ 
 
    { IdCoupon: 222 }, 
 
    { IdCoupon: 444 } 
 
    ] 
 
},{ 
 
    IdInvoiceRequest: "def", 
 
    Coupons: [ 
 
    { IdCoupon: 555 } 
 
    ] 
 
}]; 
 

 
//Loop through the invoices in the response 
 
for(var i=0; i<simulatedResponse.length; i++){ 
 
    //Loop through the coupons in the invoice 
 
    for(var j=0; j<simulatedResponse[i].Coupons.length; j++){ 
 
    //Loop through the existing coupons 
 
    for(var k=0; k<existingCoupons.length; k++){ 
 
     //If the unique identifier matches... 
 
     if(existingCoupons[k].IdCoupon == simulatedResponse[i].Coupons[j].IdCoupon){ 
 
     //Splice one existing coupon out of the array at the current index 
 
     existingCoupons.splice(k, 1); 
 
     break; 
 
     } 
 
    } 
 
    } 
 
} 
 

 
console.log(existingCoupons)

+0

它的工作原理!但是,如果您看到API的示例,请有两个“IdInvoiceRequest”。现在,这部分代码会迭代第一个IdInvoiceRequest。我如何迭代所有IdInvoiceRequest? –

+0

我更新了我的答案。基本上你只需要在两个优惠券循环之外的另一个循环,它将迭代你的回复中的发票。 –

+0

我欠你一杯啤酒!它工作完美! –

0

让我们为您尝试解决的问题的概念性定义添加一些明确性,然后选择实际的程序化实现。

看来你有2个列表,目标是从第一个列表中删除第二个列表中存在的所有条目。解决这个问题的一种可能的,相当直接的方法是创建第三个空列表,然后使用迭代循环(第一个列表的外部列表和第二个列表的内部列表)动态地添加第一个不存在的列表中的条目在第二个。除了显而易见的简单性外,该方法还将提供接近最佳的计算性能。

希望这可能有所帮助。