我是 JavaScript 和JS框架的新手。我有以下 Vuejs 代码片段:
<div v-for="coefficient in coefficients" class="coefficient">
<div>
<span class="name">name:{{coefficient.name}}</span>
<span class="value">value:{{coefficient.value}}</span>
<span>---</span>
</div>
</div>
这是输出:
name: Ubuntu
value: 1
---
name: MacOS
value: 2
---
name: Windows
value: 3
---
如何通过Vuejs排除coefficients
的最后一项?
答案 0 :(得分:4)
只需使用v-for="coefficient in coefficients.slice(0,-1)"
答案 1 :(得分:4)
您可以使用计算属性,也可以使用coefficients.slice(0, -1)
,如下所示:
new Vue({
data : {
coefficients : [
{name : "a", value : 2},
{name : "b", value : 3},
{name : "c", value : 4}]
},
el : "#app"
})
<div id="app">
<div v-for="coefficient in coefficients.slice(0, -1)" class="coefficient">
<div>
<span class="name">name:{{coefficient.name}}</span>
<span class="value">value:{{coefficient.value}}</span>
<span>---</span>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js"></script>