如何根据复选框汇总值

问题描述 投票:0回答:3

我试图将数字一起添加为汇总值,具体取决于选中哪个项目在我的结帐页面上显示为总计。

我试过了

if ( this.products[0].selected === true ) {
this.summary = this.products[0].price
} else if ( this.products[0].selected === true, this.products[1].selected === true  ) {
this.summary = this.products[0].price + this.products[1].price 
} else {
this.summary = 0;
}

显然没有完整的陈述,但它甚至没有工作到目前为止。

我有一个包含这个对象的数组

data: {
  summary: 0,
  products: [
    { name: 'Hemsida', price: 290, selected: false },
    { name: 'Copywriting', price: 190, selected: false },
    { name: 'Fotografering', price: 190, selected: false }
  ]
}

当我检查我的盒子时,它将链接到!products [0]。选中

我的最新尝试是使用switch语句,我可能已完全删除它,因为这是我在该领域的第一个switch语句:P

  priceSummary() {
    switch (
      (this.products[0].selected,
      this.products[1].selected,
      this.products[2].selected)
    ) {
      case (true, false, false):
        this.summary = this.products[0].price
        break
      case (true, true, false):
        this.summary = this.products[0].price + this.products[1].price
        break
      case (true, false, true):
        this.summary = this.products[0].price + this.products[2].price
        break
      case (false, false, true):
        this.summary = this.products[2].price
        break
      case (false, true, false):
        this.summary = this.products[1].price
        break
      case (false, false, false):
        this.summary = 0
        break
      case (true, true, true):
        this.summary =
          this.products[0].price +
          this.products[1].price +
          this.products[2].price
      default:
        this.summary = 0
    }

它做了一些事情,但它没有做它应该做的事情:P到处都是。帮助任何人???

javascript vue.js switch-statement conditional
3个回答
0
投票

最好的方法是@Nil上面说的。但是如果你想要没有循环的“不可迭代”版本,你可以这样做:

priceSummary() {
    this.summary = 0

    if(this.products[0].selected) {
        this.summary += this.products[0].price // same as this.summary = this.summary + this.products[0].price
    }

    if(this.products[1].selected) {
        this.summary += this.products[1].price
    }

    if(this.products[2].selected) {
        this.summary += this.products[2].price
    }
}

1
投票

像这样的事情:

//looping on products
this.product.forEach((prod) => {
 //checking if product is selected
 if(prod.selected === true){
  // if it's selected, adding its price to the sum
  this.summary += prod.price
 }
}

?


0
投票

如果使用数组方法,每个步骤都非常简单易读。除了一些边缘情况,switch语句不应该替换循环。

const model = {
  data: {
    summary: 0,
    products: [
      { name: 'Hemsida', price: 290, selected: true },
      { name: 'Copywriting', price: 190, selected: true },
      { name: 'Fotografering', price: 190, selected: false }
    ]
  }
};
// Create an array of only selected products.
const selected_products = model.data.products.filter( product => product.selected );
// Sum the prices of the selected products.
const price = selected_products.reduce(( sum, product ) => sum + product.price, 0 );

model.data.summary = price;

console.log( model.data.summary );
© www.soinside.com 2019 - 2024. All rights reserved.