A
A
Alex2020-05-22 18:50:05
Vue.js
Alex, 2020-05-22 18:50:05

How to filter data in a loop?

Good afternoon, there is a store with axios

import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';
Vue.use(Vuex);
export default new Vuex.Store({
    namespaced:true,
    state: {
        ListTicket: '',
    },

    getters: {
        TicketList: state => {
            return state.ListTicket.data
        },

 
        doneTodos: (state) => (id) => {

            let set;
            set = state.ListTicket.data;
            return  set.find(set => set.id === id)
        }



    },

    mutations: {
        setList(state, payload){
            state.ListTicket = payload;
        }
    },
    actions: {
        async getList({commit}){
            let response = await axios.get('/account/ticketApi/list');
            if (response.status === 200) {
                commit('setList', response.data);
            }
        }
    },
    modules: {
    }
})


return state.ListTicket.data
returns an array of objects from the base,

...mapGetters([
                'TicketList',
            ]),


I display all tickets through v-for, but for example, I need to sort the ticket by a specific id-tell me. Thanks

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
SvinkaBacilka, 2020-05-22
@SvinkaBacilka

Use a filter for arrays
This filter checks which element of the array contains the key 'available' in the value true: For id, you can do this:
array.filter(item => item.available)

array.filter((item) => {
  return item.id === 42
})

S
segio_tt, 2020-05-23
@art055

state: {
ListTicket: '',
},
getters: {
TicketList: state => {
return state.ListTicket.data
},
Where did data come from? There just should be return state.ListTicket and
ListTicket: [],

A
Alex, 2020-05-23
@astrodeep

here is what i am trying to do

GetListId: (state) => (id) => {
            return state.ListTicket.find(ListTicket => ListTicket.id === id)
        }

...mapGetters([
                'LIST',
                'GetListId'
            ]),

{{ GetListId(100) }}
and an error,
"state.ListTicket.find is not a function"
although if in the state it is test to write an object of arrays with handles, then everything works

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question