Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get unique array of objects filtering by object key Vuex?

I’m expecting to obtain a new array of ojects with a none repeatable key value trip_class. But I’m getting still 30 object items instead of a single one or two. Using computed initial array of objects Vuex.

From this :

[
    {name: 'john', trip_class: 0, lastname: 'lastname'}, 
    {name: 'Don', trip_class: 1, lastname: 'lastname'},
    {name: 'Joshua', trip_class: 1, lastname: 'lastname'},
    {name: 'Mary', trip_class: 2, lastname: 'lastname'}
]

I’d like to obtain this :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
    {name: 'john', trip_class: 0, lastname: 'lastname'}, 
    {name: 'Don', trip_class: 1, lastname: 'lastname'},
    {name: 'Mary', trip_class: 2, lastname: 'lastname'}
]
    computed: {
            flights() {
              return this.$store.getters.getFlights;
            },
            flightsClasses() {
              console.log(this.flights)    // returns an array of objects
              let flightsClasses = this.flights 
                flightsClasses.reduce((acc, obj)=>{
                  var exist = acc.find((flightClass) => obj.trip_class === flightClass );
                  if(!exist){
                    acc.push(obj);
                  }
                  return acc;
                },[]);

              console.log('flightsClasses',flightsClasses)    // returns repeatable items array
              return flightsClasses
            },

>Solution :

Does this fix it?

acc.find((flightClass) => obj.trip_class === flightClass.trip_class );

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading