json - Swift 中的 JSON 和 Node 如何在 GET 请求中实现类别过滤器
问题描述
简单地说,我想将完整的 json 数组过滤为三个类别。
让我告诉你在说什么
var menu = [
{
id: "coffe",
name: "Espresso",
description: "",
image: ''
},
{
id: "coffe",
name: "Latte",
description: "",
image: ''
},
{
id: "coffe-nosugar",
name: "Hot Chocolate",
description: "",
image: ''
},
{
id: "tea",
name: "Red Tea",
description: " ",
amount: "0.0",
image: ''
},
{
id: "tea",
name: "Green Tea",
description: " ",
amount: "0.0",
image: ''
},
{
id: "fresh-juice",
name: "Orange",
description: "",
amount: "0.0",
image: ''
}
]
现在,谁能告诉我如何将这些数据分为三类
- 咖啡
- 茶
- 果汁
现在我在我的nodeJS中使用它
app.get('/menu', (req, res) => res.json(menu))
要显示所有库存项目而无需任何过滤器,只需转到 http://APIurl/menu
解决方案
var menu: [MenuItems] = []
override func viewDidLoad() {
super.viewDidLoad()
fetchInventory { menu in
// Answer: This is will eliminate items with id == "1".
menu = (menu?.filter { $0.id != "1"})!
//Show All Items
self.menu = menu!
self.tableView.reloadData()
}
}
感谢金元中!在此处查看完整帖子:如何使用 Swift 实现数组过滤器?
推荐阅读
- apache-kafka - 使用 JMX 监控 Kafka 消费者指标
- c++ - 给定一个右值,为什么 move ctor 比 const copy ctor 更匹配?
- java - 如何将 JSON GET 请求转换为 HTTP PATCH 请求
- jq - 在多行上拆分文本时解析错误
- asp.net - 将自定义 UserManager 注入 Controller Net Core 2.1
- java - 自定义微调器下拉箭头
- file - 拆分和合并大文件所需的时间
- android - 通知自动取消不起作用按钮单击
- database - Firebase:访问数据库.....但对 onDataChange 方法感到困惑
- angular - 通过 HTTPS 在服务器上部署 Angular 6 应用程序