代码之家  ›  专栏  ›  技术社区  ›  Philipp Kishkovarov

如何从循环返回值。威斯康星州

  •  0
  • Philipp Kishkovarov  · 技术社区  · 7 年前

    抱歉,我是js的初学者,我有一个基本问题,但我花了一整天的时间在google上寻找答案,但我没有。

    我有一个基于php开发的大型金融工具,我需要构建一个复杂的金融计算器来显示所有具有反应性的东西。我需要帮助来弄清楚如何用许多 if 循环内的语句,然后对数组中每个对象的输出值求和,并返回总计值。为此使用vuejs。

    所以我的 cashDividends() 必须是循环中每个对象的计算值之和。

    下面我放了一段代码来理解我所面临的问题。

    请检查一下有没有时间。谢谢!

         new Vue({
        el: "#waterfall",
        data() {
            return {
            info: {
                cash_dividends: true,
                converted_liabilities: true,
            },
            equities: [
                @foreach($preferredEquities as $equity)
                { name: '{{ $equity->name }}', id: {{ $equity->id }} },
                @endforeach
                ]
            }
        },
        computed: {
            remainingExit () {
                return this.form.exit_value - this.form.uncovered_debt - this.form.transaction_fees
            },
            cashDividends() {
        //I supposed should be something like this.     
              this.equities.forEach(function(equity)
              {
                //Here I make a lot of calculations with bunch of if statements using object and DOM input values. for each object 
    
              }
              // And here I need to return sum of calculated values from each object (equity) in array 
            }
        },
    
    3 回复  |  直到 7 年前
        1
  •  2
  •   Boussadjra Brahim    7 年前

    你可以用 reduce 您可以进一步了解的功能 here :

    new Vue({
      el: "#app",
      data: {
        equities: [{
            name: "Serias A",
            price: 20
          },
          {
            name: "Serias B",
            price: 21
          },
        ]
      },
      computed: {
        cashDividends() {
          return this.equities.reduce(this.sum);
        }
      },
      methods: {
        sum(total, num) {
    
          return total.price + num.price
        }
      }
    });
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.2.1/vue.min.js"></script>
    <div id="app">
      {{cashDividends}}
    </div>
        2
  •  1
  •   ykaragol    7 年前

    使用 reduce 功能

    new Vue({
    el: "#waterfall",
    data: {
        equities: [
               {name: "Serias A", price: '20'},
               {name: "Serias B", price: '21'},
            ]
    },
    computed: {
        cashDividends() {
            var sum = this.equities.reduce(function(acc, equity) {
                return acc + parseInt(equity.price);
            }, 0)
        }
    }
    });
    
        3
  •  0
  •   feanor07    7 年前

    您可以定义一个变量和并迭代地添加 equity.price 具体如下:

        cashDividends() {
            let sum = 0
            this.equities.forEach(function(equity)) {
                sum+=equity.price
            }
    
            return sum
        }
    
    推荐文章