简体   繁体   中英

Axios VueJs Response Map Unique

I am trying to retrieve car manufacturer ID's using AXIOS within VueJs front-end and Spring back-end.

In the code provided below I have achieved that, however some of the ID's repeat( this is meant to happen ), the question is how is it possible to filter out the duplicate ID's?

My getMethod below.

   getMethod () {
            AXIOS.get(`/url/`)

                .then(response => {
                        if (response.status == 200) {

this.response = response.data                                     
                            this.myTest = response.data.map(mesg => mesg.carManufacterId)
                            console.log(this.myTest)

                        }
                    },
                    (err) => {                

                       if (err.response.status == 500) {

                            console.log('turned off')
                        }
                        else if (err.response.status == 404) {                         
                                console.log('Could not retrieve)
                        }
                    })

        },

You achieve that by looping through your response data array and check in each iteration if the new item is already in myTest array based on carManufacterId :

this.myTest = []
response.data.forEach(item => {

  if (!this.myTest.some(rec => {
      return item.carManufacterId === rec.carManufacterId
    })) {
    this.myTest.push(item);
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM