简体   繁体   English

在 JavaScript 中从数组中删除双引号

[英]Remove double quotes from array in JavaScript

How to remove double quotes from array in JavaScript?如何从 JavaScript 中的数组中删除双引号?

my data, this polygon coordinates google maps我的数据,这个多边形坐标谷歌地图

["{lat:-8.089057558100306,lng:112.15251445770264}", "{lat:-8.100954123313068,lng:112.15251445770264}", "{lat:-8.100954123313068,lng:112.1782636642456}", "{lat:-8.087867882261257,lng:112.17800617218018}", "{lat:-8.089057558100306,lng:112.15251445770264}"]

to

[{lat:-8.089057558100306,lng:112.15251445770264}, {lat:-8.100954123313068,lng:112.15251445770264}, {lat:-8.100954123313068,lng:112.1782636642456}, {lat:-8.087867882261257,lng:112.17800617218018}, {lat:-8.089057558100306,lng:112.15251445770264}]

after remove double quotes my data must be still array, not string.删除双引号后,我的数据必须仍然是数组,而不是字符串。

Thanks谢谢

If you can't alter the results you get above, you have to work around it.如果你不能改变上面得到的结果,你必须解决它。

 var a = ["{lat:-8.089057558100306,lng:112.15251445770264}", "{lat:-8.100954123313068,lng:112.15251445770264}", "{lat:-8.100954123313068,lng:112.1782636642456}", "{lat:-8.087867882261257,lng:112.17800617218018}", "{lat:-8.089057558100306,lng:112.15251445770264}"]; a = a.map(function(o){ var d = o.split(',').map(function(b){ return Number( b.replace(/(}|{lat:|lng:)/g, '') ); /* OR b.replace('{lat:', '') .replace('lng:', '') .replace('}', ''); */ }); return { lat: d[0], lng: d[1] }; }); console.log(a);

Iif you really trust that data, eval ing it is the simplest way to convert it: IIF你真的相信数据, eval荷兰国际集团也将其转换最简单的方法:

 let data = ["{lat:-8.089057558100306,lng:112.15251445770264}", "{lat:-8.100954123313068,lng:112.15251445770264}", "{lat:-8.100954123313068,lng:112.1782636642456}", "{lat:-8.087867882261257,lng:112.17800617218018}", "{lat:-8.089057558100306,lng:112.15251445770264}"]; console.log(data.map(s => eval('null,' + s)));

(The null, is just there to make eval treat this as an expression instead of a { block } .) null,只是为了让eval将其视为表达式而不是{ block } 。)

However, as always, eval can introduce code injection vulnerabilities if you're not sure about where the data comes from.但是,与往常一样,如果您不确定数据的来源, eval可能会引入代码注入漏洞。 And really, you should figure out how the data got this way in the first place and fix it up into a useful format there.实际上,您应该首先弄清楚数据是如何以这种方式获得的,并将其修复为有用的格式。

You can try regex你可以试试正则表达式

 var arr = ["{lat:-8.089057558100306,lng:112.15251445770264}", "{lat:-8.100954123313068,lng:112.15251445770264}", "{lat:-8.100954123313068,lng:112.1782636642456}", "{lat:-8.087867882261257,lng:112.17800617218018}", "{lat:-8.089057558100306,lng:112.15251445770264}"] arr = arr.map(function(item) { var match = item.match(/lat:(\\-?[0-9\\.]+),lng:(\\-?[0-9\\.]+)/); return {lat: +match[1], lng: +match[2]}; }) console.log(arr)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM