[英]Remove square brackets from a text using JavaScript
I have this ajax function returns data as in plain text form.我有这个 ajax function 以纯文本形式返回数据。
$.ajax({
type: 'GET',
url: "/addObstacles",
cache: false,
datatype: "text/plain",
success: function (data) {
var obstacles = data.split('\n');
for(var i = 0;i < obstacles.length;i++){
if(!obstacles[i] == ''){
console.log(obstacles[i]);
}
}
}
});
console.log(obstacles[i]):
控制台日志(障碍[i]):
[[90, 90], [90, 112], [100, 112], [100, 100], [200, 100], [200, 125], [240, 125], [240, 121]]
I want to get rid of all square brackets and the commas after every element and it should look like this:我想去掉每个元素后的所有方括号和逗号,它应该如下所示:
90,90 90,112 100,112 100,100 200,100 200,125 240,125 240,121
As this data will be the points for drawing a SVG polyline dynamically.因为该数据将是动态绘制 SVG 折线的点。 I tried many regex and strip() but no luck.
我尝试了许多正则表达式和 strip() 但没有运气。
Try with Array#flat
method尝试使用
Array#flat
方法
const arr = [[90, 90], [90, 112], [100, 112], [100, 100], [200, 100], [200, 125], [240, 125], [240, 121]]; const res = arr.flat(); //for your desire result let start = res.shift(); let end = res.pop(); let center= res.reduce((acc,b,i)=>{ let l = i+1; if(l%2 == 0){ acc.push(`${res[i-1]} ${b}`) } return acc },[]) console.log(start+','+center.join(',')+','+end)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.