Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142591

Gathering values from sub objects in Vue.JS and assigning to parent object key

$
0
0

Here's what the data "valueContainer" looks like:

{
         "totalValue": 0,
         "subValues1": [
           { "value": 20 },{ "value": 30 }
         ],
         "subValues2": [
           { "value": 10 },{ "value": 40 }
         ]
        }

I'm trying to gather the values from 'subValues1' and 'subValues2' and store them in the 'totalValue' field? I've been trying to use computed but the for loop isn't working, here's an example of trying to accumulate values from the first sub object

  computed: {
    totalValue: function() {
      let total;
      for (let v in this.valueContainer.subValues1) {
        total = total + v.value;
      }
      return total;
    }
  }

As written I'm getting NaN for the totalValue. How can I loop through the values, accumulate the values, and store them as totalValue?


Viewing all articles
Browse latest Browse all 142591

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>