最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to filters array and loop V-for in Vue.js - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

2 Answers 2

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.

  1. 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);
    })
    
  2. and in html template use v-for like below.

    <template v-for="oneItem in filters.filter(item => {item.type == 'filter'})"> 
    </template>
    
发布评论

评论列表(0)

  1. 暂无评论