Typescript - Read and return all query parameter values that matches a specific parameter

Viewed 129

From an url, I want to get all query parameter values that matches a specific parameter.

For instance, if we have a string such as

?code1=AF&code1=AE&code1=GE&code3=FW

Is it possible to query the string for all values relating to parameter code1?

For example, the above would return a list such as [AF,AE,FW]

1 Answers

You can use URLSearchParams for this use case:

const parameters = new URLSearchParams('?code1=AF&code1=AE&code1=GE&code3=FW');
const result = parameters.getAll('code1'); // ["AF", "AE", "FW"]
Related