【问题标题】:How to add some text to each paired number [duplicate]如何为每个配对号码添加一些文本[重复]
【发布时间】:2019-11-13 10:15:26
【问题描述】:

我的脚本有问题。它从 url 解析 SVG 并将数据作为日志发送到控制台。它发回的字符串在谷歌控制台中看起来像这样:[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]]

这是我尝试过的。

我的功能:

const polylines = Array.from(xmlDoc.getElementsByTagName('polyline'));

polylines.map(
pl => pl.getAttribute('points').split(' ').map(
    pair => pair.split(',').map(x=>+x),

   var paired = ("[" + (x=>+x) + "]"), // Tried adding "(x=>+x)" - result huge error.... Can't use a variable there??

console.log("[" + paired + "]")

正如我所料,这很快就以错误告终。 我似乎无法找到让我的脚本执行此操作的方法...如果您可以提供帮助,请执行此操作。这不是任何形式的重复帖子。我需要这个以特定方式登录控制台,这在此处的其他帖子中找不到。

完整脚本:(当前)

xhr=new XMLHttpRequest();
// Tell the request where the file is.
xhr.open("GET", "http://URLHERE.com/blah.svg");
// Add event handler to process the file once it's been fetched.
xhr.addEventListener("load", function() {
// Once the text is available, create an XML parser
// and parse the text as an SVG image.
const xmlDoc = new DOMParser().parseFromString(
this.responseText.trim(),
"image/svg+xml"
);
// xmlDoc.getElements() returns something Array-like, but not an Array.
// This turns it into an Array.

const polylines = Array.from(xmlDoc.getElementsByTagName('polyline'));

var Lines = (polylines.map(
pl => pl.getAttribute('points').split(' ').map(
    pair => pair.split(',').map(x=>+x),   //<<<< seperates the comma between each split.
    console.log("[" + pl.getAttribute('points') + "]") // Adds the "[", "]" to the front and back and logs the lines.
    )
));
});
xhr.send();

【问题讨论】:

  • 如果您想成对获取数组元素,请尝试在常规 for 循环中增加 2。像这样for(var i=0; i&lt;polyline.length; i=i+2) { console.log(polyline[i], polyline[i+1]) }
  • 登录谷歌控制台时。我希望它记录如下内容:[[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]
  • 我认为你可以用上面的 for 循环替换 var Lines = ... 块。
  • "272,437 274,437 274,437 275,437 275,437 277,438 277,438 280,439 280,439 281,440 281,440 282,440 283,440 284,440 285,439 286,438 287,438 287,437 287,436 287,435 288,434" 认为这是您调用pl.getAttribute('points') 时返回的字符串。它返回给我这个["272,437", "274,437", "274,437", "275,437", "275,437", "277,438", "277,438", "280,439", "280,439", "281,440", "281,440", "282,440", "283,440", "284,440", "285,439", "286,438", "287,438", "287,437", "287,436", "287,435", "288,434"] 然后你像这样再次映射.map(arg =&gt; console.log('['+arg+']'));

标签: javascript xmlhttprequest tampermonkey


【解决方案1】:

你试过这样的事情吗?

var result = "";

for(i=0; i>polylines.length; i+8) {
   result = result + "["+polylines.slice(i,i+7)+"],"
   console.log("["+polylines.slice(i,i+7)+"]");
}

SLICE方法可以得到两个参数,第一个是BEGIN,第二个是END,都和你的String的索引有关。但要小心,它将开始将 BEGIN 索引中的字母转移到 END-1 索引中的字母,即它离开 END 的索引。

var exemple = "stackoverflow";

console.log(exemple.slice(5,9));

//console: "over"

OBS:原字符串/数组不会被修改。

您可以了解更多信息,以及“三个 S”:https://medium.com/@jeanpan/javascript-splice-slice-split-745b1c1c05d2

【讨论】:

  • 它是属于你字符串[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] 跨度>
  • 这条线“const polylines = Array.from(xmlDoc.getElementsByTagName('polyline'));”之后的折线的值是多少?
  • 这是我当前的脚本,它仍然不正确jsfiddle.net/g10L9mcx/1 我需要它在控制台中像这样打印:[[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]] .. 现在是这样。它一次打印一个日志,如下所示:[287,436]
  • 从那个链接你可以看到我尝试做的事情。这只是一些复杂的东西。
  • 请修复该链接中的脚本或我在问题中发布的脚本。我只是真的希望它像这样打印数据[[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]]
猜你喜欢
  • 1970-01-01
  • 2017-04-09
  • 2020-02-05
  • 2010-11-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2016-08-15
相关资源
最近更新 更多