skip to Main Content

I am just staring react now and I am reading about queries. I have learned to search for countries but I do not know operations to be more specific.

https://somesite…./api?search=N

Returns me all the countries that has N in its name, but I want to find countries that starts with N, any ideas?

Tried with n and ^n

2

Answers


  1. Please be more specific… are gou working with some api?

    If yes provide the api… or you are building your own?

    Login or Signup to reply.
  2. Way 1:
    You need to check in API documentation for the same, whether they is any way to finding countries that start with a particular letter.

    Way 2:

    => Store the API response in one state and filter the result using startsWith() method in JavaScript. Here is the example:

    const [data,setData] = useState([])
    

    Use filter while saving the data.

    APICALL().then((res)=>{
    setData(res.data.filter(country => country.name.startsWith('N'));)
    })
    

    Let me know, if you have any doubts.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search