简体   繁体   English

如何使用 vue.js 在列表中添加 2 个数字

[英]How to add 2 numbers inside a list using vue.js

How can I add the first 5 numbers that my list take from the url and then calcuate the average.如何添加我的列表从 url 中获取的前 5 个数字,然后计算平均值。 What i mean is taking the average values per 5 minutes, 30 minutes,60 minutes.我的意思是每 5 分钟、30 分钟、60 分钟取平均值。 I made a delay 1 minute so at the first 5 minutes in my list there should be 5 values.我延迟了 1 分钟,所以在列表的前 5 分钟应该有 5 个值。 Then I want to show in 3 different templates the average of the values I take.然后我想在 3 个不同的模板中显示我所取值的平均值。

<template>
  

<div id="app" >
  
 
  <form>
    
    <label>Enter Currency: <input type="text" v-model="currency"></label>
    <input id="clickMe" type="button" value="Submit"  @click="getData(currency) "/>
    
    
    
    
  </form>
  
  <pre></pre><p>Bitcoin Value is : {{ apiData?.data?.amount }}</p>
  

  <template v-for="value in getShortList(5)">
    <pre></pre><p>Last 5 minute value is : {{ }} </p>
    <li class="divider" role="presentation"></li>
  </template>

  <template v-for="item in getShortList(29)">
    <pre></pre><p>Last 30 minute value is : {{parseInt (getShortList(29))}}</p>
    <li class="divider" role="presentation"></li>
  </template>

  <template v-for="item in getShortList(59)">
    <pre></pre><p>Last 60 minute value is : {{ parseInt (getShortList(59)) }}</p>
    <li class="divider" role="presentation"></li>
  </template>



 

  <template>
<div>
  <apexchart width="500" type="bar" :options="options" :series="amountList"></apexchart>
</div>
</template>



  
</div>



</template>

My script code is above:我的脚本代码在上面:


 import axios from 'axios'
 

export default {
  
 
 
  data() {
    return {
      apiEndPoint : 'https://api.coinbase.com/v2/prices/spot?currency=',
      apiData: [],
      amountList: [],
      
       
      
      
      
    
    }
  },
  

  created () {
   


    
    this.getData(this.currency);

   
   
  }
,
 
  

  methods: {
    getShortList(shortListSize) {
      return this.amountList.slice(0, shortListSize);
      
     

    },

    

    getData(currency) {

     
      axios.get(`${this.apiEndPoint}${currency}`).then((response) => {
        
        this.timer = setInterval(() => {
   
        this.apiData = response.data
        this.amountList.push(response.data.data.amount)
        

        console.log(this.amountList)
      }, 5000) 
        })
       
    
  
        
    
      }
      
    }
  }


  


   
</script>

As per my understanding and by the comments you added, You are facing challenge in getting the average of the array ( this.amountList ) items.根据我的理解和您添加的评论,您在获取数组 ( this.amountList ) 项目的平均值方面面临挑战。 If Yes, you can achieve that with the help of Array.reduce() method.如果是,您可以在Array.reduce()方法的帮助下实现。

Live Demo :现场演示

 // spliced amountList which contains 5 items. const arr = [100, 200, 30, 44, 50]; // Find the average const average = arr.reduce((a, b) => a + b, 0) / arr.length; // output console.log(average);

Once you get the average, You can make this reactive by assigning the value to a data object property.获得平均值后,您可以通过将值分配给数据 object 属性来使其具有反应性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM