0

I'm very beginner to javascript and need to navigate me on the right way how to use it in my task.

I have a massive 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.

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.

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

Any tips guys, just need to figure out the concept.

2
  • 1
    Forget about Vue. Do it in plain old JavaScript. Create a function that takes in your data and output s the sum. Once your calculations correct. Then use Vue to display the results. Commented Dec 20, 2018 at 22:26
  • Possible duplicate of How to return value from loop. js, vuejs Commented Dec 21, 2018 at 10:15

1 Answer 1

3

it sounds like you should use a reduce

const total = this.equities.reduce((sum, equity) => {
  const myEquityCalc = << something >>;
  return sum + myEquityCalc;
}, 0)

Demo:

const equities = [{value: 100}, {value: 200}];

const total = equities.reduce((sum, equity) => {
  return sum + equity.value;
}, 0)

console.log(total)

Sign up to request clarification or add additional context in comments.

3 Comments

Ok thanks for the respond, I'm going to make a test with it.
have undefined output
might be an issue in your calculations - I added a demo to help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.