v-for loops counter without .lenght property #12471
Replies: 4 comments 17 replies
-
It's hard to understand what you are asking.. <script setup>
import { ref } from 'vue';
const array = ref(['a', 'b', 'c', 'd', 'e', 'f']);
</script>
<template>
<div v-for="(item, i) in array">
{{ i }}: {{ item }}
<span v-if="i === 3">!!!</span>
</div>
</template> |
Beta Was this translation helpful? Give feedback.
-
Maybe like this: <script setup>
import { onBeforeUpdate } from 'vue';
import { ref } from 'vue'
const array = ref([1, 2, 3, 4, 5, 6, 7]);
let count = 0;
onBeforeUpdate(() => {
count = 0;
});
function checkCondition(item) {
if (item % 2 === 0) {
count++;
return true;
}
return false;
}
</script>
<template>
<div v-for="item in array">
{{ item }}
<span v-if="checkCondition(item)">
yes
</span>
</div>
Total: {{ count }}
</template> Things to note:
But it's not very pretty. I would not recommend doing that and instead I would definitely recommend to determine the total count in the script section and not by how often something was rendered in the template section. |
Beta Was this translation helpful? Give feedback.
-
@jiri-jiri You can mark each of your |
Beta Was this translation helpful? Give feedback.
-
Yes, you can count loops or specific items in Vue's 1. Using a Counter in the TemplateIf you want to count specific items that match a condition, you can use a counter variable directly in your template with <template>
<div>
<p v-for="(item, index) in items" :key="index">
<span v-if="item.condition">{{ incrementCount() }}</span>
{{ item.name }}
</p>
<p>Total Count: {{ count }}</p>
</div>
</template>
<script>
export default {
data() {
return {
items: [
{ name: "Item 1", condition: true },
{ name: "Item 2", condition: false },
{ name: "Item 3", condition: true },
],
count: 0,
};
},
methods: {
incrementCount() {
this.count++;
return this.count;
},
},
watch: {
items: {
handler() {
this.count = 0; // Reset count when items change
},
deep: true,
},
},
};
</script> 2. Using a Computed PropertyA cleaner approach would be to calculate the count using a computed property. This avoids the need to call a method during rendering and keeps your data reactive. <template>
<div>
<p v-for="(item, index) in items" :key="index">
<span v-if="item.condition">{{ item.name }}</span>
</p>
<p>Total Count: {{ filteredCount }}</p>
</div>
</template>
<script>
export default {
data() {
return {
items: [
{ name: "Item 1", condition: true },
{ name: "Item 2", condition: false },
{ name: "Item 3", condition: true },
],
};
},
computed: {
filteredCount() {
return this.items.reduce((count, item) => {
return item.condition ? count + 1 : count;
}, 0);
},
},
};
</script> 3. Nested
|
Beta Was this translation helpful? Give feedback.
-
Hi, is there a simple way to count loops (like in plain javascript) in v-for without using array.lenght property or filter() method? Would be nice to count specific items selected via v-if within nested v-for. Searching a lot but no answer so far. Thanks for any advice.
Beta Was this translation helpful? Give feedback.
All reactions