我正在尝试在 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)
}
})
},
最佳答案
通过遍历响应数据数组并在每次迭代中检查新项目是否已在基于 carManufacterId
的 myTest
数组中来实现:
this.myTest = []
response.data.forEach(item => {
if (!this.myTest.some(rec => {
return item.carManufacterId === rec.carManufacterId
})) {
this.myTest.push(item);
}
});
关于javascript - Axios VueJs 响应映射独特,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54116825/