[英]Get comma separated string from array
I have the following JSON in JS. 我在JS中有以下JSON。
{
"url":"http://example.com/main.aspx",
"report_template_id":"1",
"interval_secs":"86400",
"analysis_type":"lite",
"email":"rokumar@example.com",
"alerts":["num_domains", "med_load_time", "avg_load_time", "tot_req"]
}
I want the list of alerts to be removed and replaced with comma separated values as follows. 我希望删除警报列表,并用逗号分隔值替换,如下所示。
{
"url":"http://example.com/main.aspx",
"report_template_id":"1",
"interval_secs":"86400",
"analysis_type":"lite",
"email":"rokumar@example.com",
"alerts":"num_domains,med_load_time,avg_load_time,tot_req"
}
Just adding all the steps:- 只需添加所有步骤: -
1). 1)。 Taking your JSON in a variable. 将JSON放在变量中。
data = {"url":"http://example.com/main.aspx","report_template_id":"1","interval_secs":"86400","analysis_type":"lite","email":"rokumar@example.com","alerts":["num_domains","med_load_time","avg_load_time","tot_req"]};
2). 2)。 Parse JSON data to object. 将JSON数据解析为对象。 Assuming the JSON is a string initially do a typeof(data)
to be clear. 假设JSON是一个字符串,最初要确定一个typeof(data)
。
data = JSON.parse(data);
3) Change list of alerts to comma separated values 3)将警报列表更改为逗号分隔值
data.alerts = data.alerts.join(',');
4) Convert back to string 4)转换回字符串
data = JSON.stringify(data)
So data will look like 所以数据看起来像
{
"url": "http://example.com/main.aspx",
"report_template_id": "1",
"interval_secs": "86400",
"analysis_type": "lite",
"email": "rokumar@example.com",
"alerts": "num_domains,med_load_time,avg_load_time,tot_req"
}
Note:- If you will just say join()
then also it will work, because default delimiter is ,
only, just to clarify I have given that. 注: -如果您只想说join()
,然后也将工作,因为默认的分隔符是,
只是,只是为了澄清我已经考虑到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.