简体   繁体   English

如何在 function 中使用引用嵌套 function 内部参数的条件语句在 javascript 中返回结果?

[英]How can use a conditional statement inside of a function that references the parameter inside of a nested function to return a result in javascript?

I have a shopping cart where I need to be able to add a conditional discount to a function that currently gets the price of an item and multiplies it by the quantity.我有一个购物车,我需要能够向 function 添加有条件的折扣,该 function 当前获取商品的价格并将其乘以数量。 Currently the function works like a charm without the discount however the discount needs to be able to tell if there are two or more items with the same "tag" (ex. "cheese pizza", "mushroom pizza", "Hawaiian pizza") and then subtract an amount from the returned total for it to work.目前,function 就像没有折扣的魅力,但折扣需要能够判断是否有两个或多个具有相同“标签”的商品(例如“奶酪披萨”、“蘑菇披萨”、“夏威夷披萨”)然后从返回的总数中减去一个金额以使其工作。 How do I achieve this?我如何实现这一目标?

This function gets the total price amount of all items items in the user's cart这个 function 获取用户购物车中所有商品项目的总价格金额

 get totalAmount() {
 let total = 0;
 this.cart.forEach(item => (total += this.getItemTotal(item)));

 ///// possible conditional statement here
 ////// something like  if (item.tags == "cheese"){
 /////// return total - 2;   }
 //////// ( currently just errors out to "**item not found**" even though I thought   
 ///// since the parameter "item" was in the function within the function it could recognize it)             


return total;
}

This function is inside the above function.这个 function 在上面的 function 里面。 It is used to get an individual item's sub total including it's options它用于获取单个项目的小计,包括它的选项

getItemTotal(item) {
 let total = item.price * item.quantity;

  item.options.forEach(option => (total += option.value * item.quantity));
  return total;
  }

Below is an example of an item with a "tag" of cheese.下面是一个带有奶酪“标签”的项目示例。 The conditional block needs to determine if there are two tags which are similar like that of cheese and then take away $2 from the total in the first function条件块需要判断是否有两个标签与奶酪相似,然后从第一个 function 中的总和中取出 2 美元

     "item": [
     {
     "id": 1,
     "guid": "1d4aa3b2-c059-4fa7-a751-9bca735e4ea1",
     "thumb": "https://foodorderingapp9309.s3-us-west- 
     1.amazonaws.com/CheesySenstions/menu/CheesePizza.JPG",
     "title": "Cheese",
     "body": "try new cheese pizza",
     "tags": ["pizza"],
     }
     ]

You can access the item inside the forEach loop callback.您可以访问forEach循环回调中的项目。

You can use a counter to count the number of items with a specific tag.您可以使用计数器来计算具有特定标签的项目数。 Here is an example:这是一个例子:

get totalAmount() {
  let total = 0;
  let numberOfItemsWithCheeseTag = 0;

  this.cart.forEach(item => {
    total += this.getItemTotal(item);

    // Increment the counter if 'cheese' is one of the tags
    if (item.tags.includes('cheese')) {
      numberOfItemsWithCheeseTag += 1; // 
    }
  });

  // Apply the discount if the counter reached a threshold
  if (numberOfItemsWithCheeseTag >= 2) {
    total -= 2;
  }        

  return total;
}

Here's a different take这是一个不同的看法

get totalAmount() {
    let total = this.cart.reduce((t, item) => t + this.getItemTotal(item), 0)
    const count = this.cart.reduce((c, item) => c + item.tags.includes('cheese'), 0)

    if (count >= 2) {
        return total -= 2
    }

    return total
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 条件语句中的返回函数值 - return function value inside conditional statement 在if语句中使用javascript函数 - Use of javascript function inside if statement 如何在 if 语句中使用 function? - How can i use a function inside an if statement? 重构此函数以在 if 语句内部和外部一致地使用“return” Javascript - Refactor this function to use “return” consistently inside and outside of if statement Javascript 如何在javascript中的.map函数中拆分嵌套的三元语句? - How to split up a nested ternary statement inside a .map function in javascript? function 中的 return 语句有什么用? - What is the use of return statement inside a function? 如何使用内部嵌套的Parse.Promises处理javascript函数的返回 - how to handle the return of a javascript function with nested Parse.Promises inside Javascript 上的 function 中的 if 语句 - If statement inside function on Javascript 我可以使用在 function 中声明的变量作为嵌套在 function 中的另一个 function 的参数吗? - can I use variable declared within a function as a parameter for another function nested inside the function? 如何使用用户输入作为参数在 JS 中的 function 内创建条件语句? - How to create conditional statement inside of a function in JS using the user input as parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM