Is there a way to declare temporary variable in a VueJS template? My problem is that in a v-for i check if the actual value exists in an Array and if not I print another text, but for each element of the v-for it will do this twice (firstly to check if exists, and then to print the value) where with a variable it could be done once.
I'm using VueJS2 without any preprocessor nor template engines.
Here it's my array :
let formats = [
{id: 1, label: "A3"},
{id: 1, label: "A4"},
{id: 1, label: "A5"},
{id: 1, label: "A6"},
];
Here is my template :
<tr v-for="data in ajaxDatas">
<td>
<template
v-if="formats.find(e => {
return (e.shortEdge == data.height && e.longEdge == data.width) || (e.shortEdge == data.width && e.longEdge == data.height);
})"
>
{{ formats.find(e => {
return (e.shortEdge == data.height && e.longEdge == data.width) || (e.shortEdge == data.width && e.longEdge == data.height);
}).label }}
</template>
<template v-else>
Other format
</template>
</td>
</tr>
As you can see it's not really optimized... I was thinking in a way to make a temporary variable to stock the find return, something like this :
<tr v-for="data in ajaxDatas">
<td>
{{
var f = formats.find(e => {
return (e.shortEdge == data.height && e.longEdge == data.width) || (e.shortEdge == data.width && e.longEdge == data.height);
});
f ? f.label : 'Other format'
}}
</td>
</tr>
FOR THE MOMENT i found a way but it's not working properly, i can make something like this :
<tr v-for="data in ajaxDatas">
<td>
{{ f = formats.find(e => {
return (e.shortEdge == data.height && e.longEdge == data.width) || (e.shortEdge == data.width && e.longEdge == data.height);
}) }}
{{ f ? f.label : 'Other format' }}
</td>
</tr>
But this is actually printing me result of f in the HTML ... So i put it into an undisplayed html tag but it's make me think there's something better to do here...