【发布时间】:2020-04-21 06:46:49
【问题描述】:
嘿,伙计们有一些 JSON 数据和 API 试图找出过滤其类别的 API,目前只有“食物和物品”。这里是数据。
{
"id": 1587428052314,
"_id": "5e9e5599a3f3e540e9c6553c",
"Title": "Home Cleaning Sanitiser Box",
"Description": "This box has everything you need - right now!"
"Phone": "021881821921",
"Category": "food"
}
这里是api:localhost:4000/api/user-listing/
我可以在我的.then 的承诺链中以某种方式过滤它吗?
Axios.get("localhost:4000/api/user-listing")
.then((res) => {
// in here ?? this.setState({ listings: res.data });
});
干杯
【问题讨论】:
-
如果您提供的 API 返回与用户相关的所有数据,并且您想使用
"Category": "food"过滤所有数据,您可以通过filter数组助手轻松完成此操作,如果有请告诉我麻烦了。 -
请附上Minimal, Complete, and Reproducible 的尝试示例。
-
好吧,我正在使用与 axios 的反应,我了解如何获得推送等,所以当我选择食物类别并转到组件时,我猜我只是做一个 componentdimount() 然后获取所有数据然后使用 .filter 过滤它并获得所有“食物”类别?
-
类似的东西。您总是使用相同的过滤器类别,还是动态的?
-
相同的,我可以在.then 内做吗? axios.get("localhost:4000/api/user-listing").then((res) => { // 在这里 this.setState({ Listings: res.data }); }); };