I
I
ilonikso2020-12-11 21:28:28
JSON
ilonikso, 2020-12-11 21:28:28

JSON SERVER - How to send a request to filter on multiple keys?

Good evening, I am developing a small application and for testing I use Fake REST API in the form of JSON Server.
https://github.com/typicode/json-server

I have a json in this format and I want to filter to find all items that match either by name or by author.

{
    "packs": [
        {   "id": 1,
            "name": "Смешанный by Matt(1)",
            "author": "Matt",
            "imageUrl": "",
            "packUrl": "",
            "date": "2017-12-04 00:00:00",
            "difficulty": 5,
            "category": "Смешанные",
            "likes": 214,
            "size": 21851793,
            "commentId": 37
        },
        {
            "id": 2,
            "name": "Название пакета",
            "author": "Автор",
            "imageUrl": "",
            "packUrl": "",
            "date": "2016-12-04 00:00:00",
            "difficulty": 8,
            "category": "Общие вопросы",
            "likes": 2,
            "size": 2185173,
            "commentId": 34
        },
        
    ]
}


PS The simple use of the q="search" search construct is not suitable, because the search goes through all the keys in the object, both by reference and by date, and an incorrect result is possible.
Is it possible to combine filtering by name and author in one query?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ice, 2020-12-11
@IceRD

What is the difficulty, the example from the documentation does not work?


Filter
Use . to access deep properties
GET /posts?title=json-server&author=typicode
GET /posts?id=1&id=2

or do you want to process the received data?
then you can
result['packs'].filter((el)=>{return el.name == 'Название пакета' || el.author == 'Matt'})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question