admin管理员组

文章数量:1379483

I got myself into a big mess trying to add support for arrays in query arguments for an API.

The way I am handling this now is join array by commas

const params = new URLSearchParams()
params.append("arrParam", arr.join(","))

And on the sever I am splitting by comma. This works just of one dimension arrays.

Problem now is that I have an argument that is supposed to be a tuple, like ["abc", 0] And I should be able to pass array of tuples too, in the query. That means I need to support multidimensional arrays.

Is there an abstract way of doing this, that would work for any array/array of tuples, without adding a "special case" on both client and server?

I got myself into a big mess trying to add support for arrays in query arguments for an API.

The way I am handling this now is join array by commas

const params = new URLSearchParams()
params.append("arrParam", arr.join(","))

And on the sever I am splitting by comma. This works just of one dimension arrays.

Problem now is that I have an argument that is supposed to be a tuple, like ["abc", 0] And I should be able to pass array of tuples too, in the query. That means I need to support multidimensional arrays.

Is there an abstract way of doing this, that would work for any array/array of tuples, without adding a "special case" on both client and server?

Share Improve this question asked Mar 19 at 13:53 AlexAlex 66.2k185 gold badges460 silver badges651 bronze badges 3
  • Tuples aren't native to JS; how exactly are you implementing them? What about passing it as JSON? – mykaf Commented Mar 19 at 13:59
  • 1 I am using typescript for code readability so its fairly easy to work with tuples in js. And zod for validation. It's just the url search I have trouble with – Alex Commented Mar 19 at 14:05
  • 1 So you are absolutely sure that none of these arguments will ever contain a comma? Because if not, your approach will fail at that point. – C3roe Commented Mar 19 at 14:15
Add a comment  | 

2 Answers 2

Reset to default 2

Query parameters can be repeated, it's explicitly allowed. So this can be your first dimension of your array, and each value can be comma separated:

const params = new URLSearchParams();
params.append('a', '1,2');
params.append('a', '3,4');
console.log(params.toString());

Now hopefully your server side is properly implemented to parse query parameters into a multi-dict or otherwise treat repeated parameters correctly, to allow you to unparse it easily into an array of two-tuples.

To represent even more dimensions… rethink what you're doing. You'd probably want to POST a JSON request body then.

You should use JSON to serialize data, but don't fet to escape it to use in URL when constructing a string.

var data = [
  [1, 2, 3],
  ["a", {
    foo: "bar"
  }],

]

var stringified = JSON.stringify(data)
var escaped = encodeURIComponent(stringified)

const paramsString = "obj=" + escaped;
const searchParams = new URLSearchParams(paramsString);

var json = searchParams.get("obj")
var original_data = JSON.parse(json)
console.log(original_data)
.as-console-wrapper {min-height: 100%}

本文标签: javascriptHandling tuples in URL queryStack Overflow