问题描述
这真的让我感到不安.我需要对此做些更好的解释.我正在尝试以一种特定的方式使我的脚本登录google的控制台.我有一些图片可以帮助解释..让我们跳进去.
So this is really getting on my nerves. I needed to explain this a bit better so here it is. I'm trying to make my script log in google's console a specific way. I've got some images to help explain.. Let's hop on in.
首先,这是当前的记录方式.到目前为止,这很好.我对此很满意.它不是一个大阵列,美观而整洁.下图:( https://i.gyazo.com/81fc8d76b34a81f4fff7fc23e94f1bf1.png )
So first, this is how it's currently logging.. Which is good so far. I'm happy with this. It's not one big array, nice and neat.Image below:(https://i.gyazo.com/81fc8d76b34a81f4fff7fc23e94f1bf1.png)
所以这是发出的最后一个日志(您可以在上图中看到.)它只是按照我需要它进行记录的方式进行了 [[346,453],[346,452],[346,452],[346,453],[346,453],[347,453],[347,453],[347,454],[348,454],[349,454],[350,454],[351,454],[352,454],[353,454],[354,454],[354,453],[355,452],[355,453]]
So this is the last log emitted(You can see in the image above.) It's just edited to the way I need it to log:[[346,453],[346,452],[346,452],[346,453],[346,453],[347,453],[347,453],[347,454],[348,454],[349,454],[350,454],[351,454],[352,454],[353,454],[354,454],[354,453],[355,452],[355,453]]
所以我试图使所有日志都这样发送.^^用逗号替换每对之间的空格,并在每对之间添加"["和]".
So I'm trying to make all the logs send just like that.^^ Replacing the spaces between each pair with a comma, and adding "[" and "]" to each pair.
所以我希望它如何记录:(真的有帮助)( https://i.gyazo.com/af179df0b2ce93f018809f6921bce59a.png )
So how I would like it to log:(Really helps)(https://i.gyazo.com/af179df0b2ce93f018809f6921bce59a.png)
我的脚本:
xhr=new XMLHttpRequest();
xhr.open("GET", "http://myurl.com/someURLWITH.svg");
xhr.addEventListener("load", function() {
const xmlDoc = new DOMParser().parseFromString(
this.responseText.trim(),
"image/svg+xml"
);
const polylines = Array.from(xmlDoc.getElementsByTagName('polyline'));
var Lines = (polylines.map( pl => pl.getAttribute('points').split(' ').map(
pair => pair.split(',').map(x=>+x),
console.log("[" + pl.getAttribute('points') + "]")
)
));
});
xhr.send();
请记住!!!我不希望它成为一个大型阵列!!!!它分别记录每个日志的方式非常完美..我只需要更改它记录每个日志的方式.
Keep in mind!!! I don't want it to be one big array!!!! The way it logs each one separately is perfect.. I just need to change the way it logs each one.
所以在我的代码中,您可以看到 console.log("[" + pl.getAttribute('points')+]")
我试图使它像这样console.log("[" + pl.getAttribute("[" +'points'+]")+]")
真的在祈祷这样做会成功,但我错了.像往常一样.
So in my code you can see console.log("[" + pl.getAttribute('points') + "]")
I've tried to making it like this console.log("[" + pl.getAttribute("[" + 'points' + "]") + "]")
really praying this would do the trick but I was wrong. Like always.
是的,只是代替它像这样记录 [346,453 346,452 346,452 346,453 346,453 347,453 347,453 347,454 348,454 349,454 350,454 351,454 352,454 353,454 354,454 354,453 355,452 355,453
...我希望它像这样记录 [[346,453],[346,452],[346,452],[346,453],[346,453],[347,453],[347,453],[347,454],[348,454],[349,454],[350,454],[351,454],[352,454],[353,454],[354,454],[354,453],[355,452],[355,453]]
谢谢.
So yeah, simply instead of it logging like this [346,453 346,452 346,452 346,453 346,453 347,453 347,453 347,454 348,454 349,454 350,454 351,454 352,454 353,454 354,454 354,453 355,452 355,453]
... I would like it to log just like this [[346,453],[346,452],[346,452],[346,453],[346,453],[347,453],[347,453],[347,454],[348,454],[349,454],[350,454],[351,454],[352,454],[353,454],[354,454],[354,453],[355,452],[355,453]]
Thank you.
推荐答案
您的代码几乎在那里,但是您登录到控制台的内容却很可笑,因为它只是未修改的points属性
Your code was almost there, but what you were logging to the console was ridiculous, since it was just the points attribute, unmodified
所以,像这样的东西
const polylines = Array.from(xmlDoc.getElementsByTagName('polyline'));
const Lines = polylines.map(pl => pl.getAttribute('points').split(' ').map(pair => pair.split(',').map(Number)));
Lines.forEach(line => console.log(JSON.stringify(line)));
或者,鉴于Array.from的第二个参数是一个映射函数,您可以进一步简化代码到
Or, given that the second argument to Array.from is a map function, you can further simplify the code to
const Lines = Array.from(xmlDoc.getElementsByTagName('polyline'), pl => pl.getAttribute('points').split(' ').map(pair => pair.split(',').map(Number)));
Lines.forEach(line => console.log(JSON.stringify(line)));
注意,console.log在处理之后完成
Note, the console.log is done AFTER the processing
至于评论
Lines.forEach(line => {
// here you can post each line to whatever you need
});
这篇关于无法修复我的脚本以记录所需的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!