【问题标题】:Get lowest price from vue js array从 vue js 数组中获取最低价格
【发布时间】:2017-03-16 03:32:50
【问题描述】:

我有这个数组,它循环遍历列表的所有行程。

 <span v-for="price in listing.trips">
       <div class="price">{{ price.cost }} </div>
</span>

是否可以应用过滤器,只为乘坐价格最低的旅行?

我试过了:

 <span v-for="price in listing.trips">
                        <div class="price">{{ Math.min(price.cost) }} </div>
                    </span>

【问题讨论】:

    标签: javascript arrays laravel vue.js


    【解决方案1】:

    也许你可以只创建计算属性?

    new Vue({
        ....
        computed: {
            cheapestTrip: function () {
                return this.listing.trips.sort(trip=>trip.price)[0];
            }
        }
    });
    

    然后像这样使用它:

    <span>
       <div class="price">{{ cheapestTrip }} </div>
    </span>
    

    如果你想要数组,你也可以省略[0]。在这种情况下,请使用您的视图 sn-p。

    【讨论】:

      猜你喜欢
      • 2018-10-12
      • 2018-06-24
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-21
      • 2015-11-05
      相关资源
      最近更新 更多