I am trying to loop an array levels using Vue.js, I could to get the response json in actions with function forEach like:
let filters = []
const array = response.data
array.forEach((element) => {
filters.push(element)
})
then stored data in state, there any way to filter by TYPE and then loop Data somehow to render in a select, data...etc. using V-for in Vue.js?
My json file:
[
{
'type': 'filter',
'datatype': 'str',
'data': [
{
'var_name': 'Gender',
'options': [
'Male',
'Female'
]
}
]
},
{
'type': 'filter',
'datatype': 'date',
'data': [
{
'var_name': 'From',
'options': []
}
]
},
{
'type': 'range',
'datatype': 'date',
'data': [
{
'var_name': 'From',
'options': []
},
{
'var_name': 'To',
'options': []
}
]
}
]
I am trying to loop an array levels using Vue.js, I could to get the response json in actions with function forEach like:
let filters = []
const array = response.data
array.forEach((element) => {
filters.push(element)
})
then stored data in state, there any way to filter by TYPE and then loop Data somehow to render in a select, data...etc. using V-for in Vue.js?
My json file:
[
{
'type': 'filter',
'datatype': 'str',
'data': [
{
'var_name': 'Gender',
'options': [
'Male',
'Female'
]
}
]
},
{
'type': 'filter',
'datatype': 'date',
'data': [
{
'var_name': 'From',
'options': []
}
]
},
{
'type': 'range',
'datatype': 'date',
'data': [
{
'var_name': 'From',
'options': []
},
{
'var_name': 'To',
'options': []
}
]
}
]
Share
Improve this question
asked Jul 18, 2020 at 2:33
LeoLeo
771 gold badge1 silver badge7 bronze badges
2 Answers
Reset to default 7<div v-for="filter in filters">... </div>
puted: {
filters() {
return this.data.filter(item => item.type === 'filter')
}
}
You can use below code in .vue file.
save your above filters array to data variable in vue file.
data() { return { filters: [] } } const array = response.data; array.forEach((element) => { this.filters.push(element); })
and in html template use v-for like below.
<template v-for="oneItem in filters.filter(item => {item.type == 'filter'})"> </template>