【问题标题】:Axios VueJs Response Map UniqueAxios VueJs 响应图唯一
【发布时间】:2019-01-09 19:15:01
【问题描述】:

我正在尝试在 VueJs 前端和 Spring 后端中使用 AXIOS 检索汽车制造商 ID。

在下面提供的代码中,我已经实现了这一点,但是一些 ID 重复(这是注定要发生的),问题是如何过滤掉重复的 ID?

下面是我的getMethod。

   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)
                        }
                    })

        },

【问题讨论】:

  • 相似但不重复
  • 您的问题是“如何从数组中删除重复项?”,这与“如何获取数组中的唯一值?”相同。

标签: javascript vue.js vuejs2 axios


【解决方案1】:

您可以通过遍历您的响应数据数组并检查每次迭代是否新项目已经在基于carManufacterId 的myTest 数组中来实现:

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

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

【讨论】:

    猜你喜欢
    • 2020-07-03
    • 1970-01-01
    • 2020-03-22
    • 2020-03-05
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 2019-06-05
    相关资源
    最近更新 更多