如何从循环中返回值.js, vuejs [英] How to return value from loop. js, vuejs

查看:38
本文介绍了如何从循环中返回值.js, vuejs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,我是 JS 初学者,我有一个基本问题,但我花了一整天试图在 google 中找到答案,但我没有.

Sorry, I'm beginner with JS,i have a basic question, but i spent a whole day trying to find answer in google and i didn't.

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

I have a massic financial instrument developed on php and I need to build complex financial calculator that shows everything with reactivity. I need help to figure out how to make complex calculations with many if statements inside of the loop and then sum output value from each object in array and return total summed value. Using Vuejs for this.

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

So my cashDividends() must be a sum of calculated values from each object in the loop.

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

Below I put a piece of code to understand problem I'm facing.

请检查是否有时间.谢谢!

Please check if have a minute. Thanks!

     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 
        }
    },

推荐答案

您可以定义一个变量 sum 并迭代地将 equity.price 添加到其中,如下所示:

You can define a variable sum and iteratively add equity.price to it as follows:

    cashDividends() {
        let sum = 0
        this.equities.forEach(function(equity)) {
            sum+=equity.price
        }

        return sum
    }

这篇关于如何从循环中返回值.js, vuejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆