【问题标题】:Getting the highest value of an JSON array and storing it in Data Vue获取 JSON 数组的最大值并将其存储在 Data Vue 中
【发布时间】:2018-02-09 23:28:36
【问题描述】:

在我的 vue 组件中,我有一个 json 出价数组。

我需要从数组中获取最高出价并将其存储到另一个变量中。

我的vue组件数据:

data() {
  return {
    bids: [],
    bid: null,

    veiling_id: this.id,
    showInput: this.auth_check,

     highestBid: null,
  }
},

从数据库中获取出价并存储到出价中。

mounted(){
    var veilingid = this.veiling_id;
    var vm = this;
    setInterval(function(){
        axios.get('/veilingen/' + veilingid + '/getbid').
        then(function (response){
            vm.bids = response.data;
        }).catch(function(error) {
            console.log(error);
        });
     }, 1000);

 }

现在我如何循环出价,获得最高出价并将其存储到最高出价中?

另外,存储代码的最佳位置是什么?

AXIOS 在mounted() 或其他地方获取请求之后?

对不起,我是 Vue 的新手。

任何帮助表示赞赏..

请求的 JSON:

"[{"id":1,"veiling_id":1,"bid":"100.00","user_id":2,"created_at":"2017-08-31 20:24:20","updated_at":"2017-08-31 20:24:20"},{"id":2,"veiling_id":1,"bid":"40.00","user_id":2,"created_at":"2017-08-31 20:43:11","updated_at":"2017-08-31 20:43:11"},{"id":3,"veiling_id":1,"bid":"3.00","user_id":2,"created_at":"2017-08-31 20:43:34","updated_at":"2017-08-31 20:43:34"},{"id":4,"veiling_id":1,"bid":"4.34","user_id":2,"created_at":"2017-08-31 20:44:32","updated_at":"2017-08-31 20:44:32"},{"id":5,"veiling_id":1,"bid":"900.00","user_id":2,"created_at":"2017-08-31 20:44:49","updated_at":"2017-08-31 20:44:49"},{"id":6,"veiling_id":1,"bid":"90.00","user_id":2,"created_at":"2017-08-31 20:51:55","updated_at":"2017-08-31 20:51:55"},{"id":7,"veiling_id":1,"bid":"90.00","user_id":2,"created_at":"2017-08-31 20:53:10","updated_at":"2017-08-31 20:53:10"},{"id":8,"veiling_id":1,"bid":"3.00","user_id":2,"created_at":"2017-08-31 20:53:18","updated_at":"2017-08-31 20:53:18"},{"id":9,"veiling_id":1,"bid":"3.00","user_id":2,"created_at":"2017-08-31 20:53:59","updated_at":"2017-08-31 20:53:59"},{"id":10,"veiling_id":1,"bid":"50.00","user_id":2,"created_at":"2017-08-31 21:03:17","updated_at":"2017-08-31 21:03:17"},{"id":11,"veiling_id":1,"bid":"1000.00","user_id":2,"created_at":"2017-08-31 21:05:35","updated_at":"2017-08-31 21:05:35"},{"id":12,"veiling_id":1,"bid":"2000.00","user_id":2,"created_at":"2017-09-01 00:07:19","updated_at":"2017-09-01 00:07:19"},{"id":13,"veiling_id":1,"bid":"3.00","user_id":1,"created_at":"2017-09-01 00:28:56","updated_at":"2017-09-01 00:28:56"}]"

【问题讨论】:

    标签: arrays json vue.js vuejs2 vue-component


    【解决方案1】:

    这是computed property 的一个很好的用例。

    computed:{
      highestBid(){
        if (this.bids.length == 0) return 
        return this.bids.reduce((a,b) => Number(a.bid) > Number(b.bid) ? a : b)    
      }
    },
    

    这是一个如何工作的示例。

    console.clear()
    
    const bids = [
      {bid: 2000},
      {bid: 1000},
      {bid: 3000},
      {bid: 4000},
      {bid: 100},
      
    ]
    
    new Vue({
      el: "#app",
      data() {
        return {
          bids: [],
        }
      },
      computed:{
        highestBid(){
          if (this.bids.length == 0) return 
          return this.bids.reduce((a,b) => Number(a.bid) > Number(b.bid) ? a : b)     
        }
      },
      mounted(){
        setTimeout(() => {
          this.bids = bids
        }, 250)
      }
    })
    <script src="https://unpkg.com/vue@2.4.2"></script>
    <div id="app">
      {{highestBid}}
    </div>

    在上面的示例中,每当bids 被填充或更改时,highestBid 将返回具有最高值的出价对象。 setTimeout 只是模拟 ajax 调用。

    您可以在 mount 或 created 中执行您的 API 调用。

    请注意,我注意到您将出价值作为字符串返回,因此代码使用Number() 将它们转换为数字。有许多方法可以在 javascript 中转换为数值,您可以研究最适合您的方法。

    【讨论】:

    • 我无法让if (this.bids.length == 0) return this.bids.reduce((a,b) =&gt; Number(a.bid) &gt; Number(b.bid) ? a : b) 工作,最高出价为 2000 时返回 900
    • @JordyGroote 你能提供一个 JSON 的实际例子吗?我会更新它。
    • 我更新了 JSON 在底部的答案。 @伯特
    • @JordyGroote 嗯。浏览器问题?这似乎工作正常。 codepen.io/Kradek/pen/mMvyVB?editors=1010
    • 我明白了,啊 Vue 太难学了。现在它完全消失了,什么也没有回来……挣扎!!!!
    【解决方案2】:

    试试这个代码。

     //params is object
     findWinConfirmed(topConfirmed) {
      let findTop = [];
      topConfirmed.data.forEach(obj => findTop.push(obj.bid));
      return Math.max(...findTop);
    }
    

    希望是帮助。

    【讨论】:

      猜你喜欢
      • 2021-07-17
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 2020-05-13
      • 1970-01-01
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多