v-for loops counter without .lenght property #12471
Unanswered
jiri-jiri
asked this question in
Help/Questions
Replies: 2 comments 12 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.
5 replies
-
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.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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