首页 > 解决方案 > 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

标签: jsonnode.jsswiftalamofire

解决方案


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 实现数组过滤器?


推荐阅读