如何在设置状态之前从我的 reponse.data 中删除不良对象



在设置状态之前,可以从响应数据中删除对象吗?

我想删除任何postContent: ""imgUrl: ""的对象。当对象显示时没有图像&&内容时,它看起来不太好。

杰森: https://phantombuster.s3.amazonaws.com/YRrbtT9qhg0/3jhBGwtSbaEV6lqijSmpoQ/result.json

我如何存储数据:

private getProfiles() {
axios
.get(
"https://cors-anywhere.herokuapp.com/" +
"https://phantombuster.s3.amazonaws.com/YRrbtT9qhg0/3jhBGwtSbaEV6lqijSmpoQ/result.json"
)
.then(response => {
this.setState({
profiles: response.data
});
})
// Error catching
.catch(errors => this.setState({ errors, isLoading: false }));
}

可以使用筛选器数组方法删除不需要的项目。

private getProfiles() {
axios
.get(
"https://cors-anywhere.herokuapp.com/" +
"https://phantombuster.s3.amazonaws.com/YRrbtT9qhg0/3jhBGwtSbaEV6lqijSmpoQ/result.json"
)
.then(response => {
this.setState({
profiles: response.data.filter(item => item.postContent && item.imgUrl)
});
})
// Error catching
.catch(errors => this.setState({ errors, isLoading: false }));
}

您可以在设置状态之前操作数组中的对象项:

.then(response => {
var cleanData = response.data.filter(item => {
return (item.postContent && item.imgUrl)
});
this.setState({
profiles: cleanData
});
})

最新更新