[英]How do I pass array as a key in JavaScript object?
I have an API that expects specialities[]
in the request body.我有一个 API 期望在请求正文中包含 specialities specialities[]
。 The problem is that I don't know how to pass this as key in javascript.问题是我不知道如何将其作为 javascript 中的密钥传递。
This is what I'm doing:这就是我正在做的事情:
const data = {
name: name,
phone: phone,
email: registerEmail,
pmc_number: pmcNumber,
speciality[]: speciality.split(","),
city: city
}
const {res} = await axios.post("api-url", data);
But, speciality[]
gives syntax error.但是, speciality[]
给出了语法错误。 So, is there any way I can send the data to the API (the API can't be changed. I have to find the solution from the client side).那么,有什么办法可以将数据发送到 API(API 无法更改。我必须从客户端找到解决方案)。 Thanks.谢谢。
'specialities': speciality.split(',')
describes the property using quotes and not a name, like in JSON. 'specialities': speciality.split(',')
使用引号而不是名称来描述属性,例如 JSON。However, it is not passing a list/array.但是,它没有传递列表/数组。
String([1,2])=='1,2';
数组是字符串化的:例如, String([1,2])=='1,2';
. . So String([])==''
, and in a property, which is a string or symbol type, it does not make a difference if you don't try smth like (1.)所以String([])==''
,在一个字符串或符号类型的属性中,如果你不尝试像 (1.)Have a blessed day!有一个幸福的一天!
You can put quotes around the key.您可以在键周围加上引号。
'specialities[]': speciality.split(","),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.