I've successfully implemented a Vue filter that restricts the length of an array to n elements. It functions perfectly when used like this:
{{ array | limitArray(2) }}
Now, I'm attempting to utilize it within a v-for
loop as follows:
<li v-for="item in items | limitArray(3)">...</li>
However, this approach is resulting in errors. Is there a way to apply a filter inside a v-for
?
Edit: Though likely insignificant, here's the code for the filter being referenced:
Vue.filter('limitArray', function (arr, length = 3) {
if (arr && arr.length) {
if (length == -1) {
return arr;
}
if (length > arr.length) {
return arr;
}
return arr.slice(0, length);
}
return null;
});