.map inside call function react native

Viewed 28

I want to send the values ​​in the DiscountPrice array to the setOrderDETAIL function as parameters. Since I call the function in .map, it calls as many functions as the item number, but I want to run this function 1 time. So I want to send the values ​​inside the DiscountPrice array to this.setOrderDETAIL function.

DiscountPrice.map((price)=>{
  this.UserCart.map((item)=>{
      return  this.setOrderDETAIL(item.Quantity, price, item.Quantity,item.ProductId,rsp.Order_Id);
  })
  })
1 Answers

You should be able to do this:

// assuming what you want is DiscountPrice = [arg1, arg2, arg3...]
DiscountPrice = ["arg1", "arg2", "arg3"]
this.UserCart.map((item) => {
    return this.setOrderDETAIL(item.Quantity, item.ProductId, rsp.Order_Id, ...DiscountPrice);
});

Notice the ... destructures the values in DiscountPrice so you can access them as individual arguments.

setOrderDETAIL would have the function signature like:

function setOrderDETAIL (quantity, productId, orderId, ...args) {
    // You can access DiscountPrice by doing args[0], args[1]...
}

Tell me if I misunderstood what you meant...(I feel like I might have. I will edit if appropriate.)

Related