在TypeScript中的数组分割函数

huangapple go评论68阅读模式
英文:

Split function on arrays in TypeScript

问题

我正在尝试在字符串上使用 split 函数,我正在使用的分隔符是 "/"。字符串中有多个 /,我想要使用最后一个 / 进行分割。

let url = https://learn.edu/d2l/login?sessionExpired=1&target=%2fd2l%2fhome     
let beforeLastSlash, afterLastSlash = url.split("/")

目前 beforeLastSlash = "https://learn.edu"afterLastSlash = "d2l"。我看了一下 MDN 文档,发现 .split() 方法有一个 limit 参数,但我不太确定如何设置它,如果我不知道字符串中有多少个 /。即使我尝试使用 url.split("/",1) 而不是 url.split("/"),我的 afterLastSlash 值仍然是 undefined

英文:

I am attempting to use the split function on a string, the seperator I am using is "/". There are multiple occurrences of / and I would like to split using the last /.

let url = https://learn.edu/d2l/login?sessionExpired=1&target=%2fd2l%2fhome     
let beforeLastSlash, afterLastSlash = url.split("/")

Currently the beforeLastSlash = "https://learn.edu" and afterLastSlash = "d2l". I was reading mdn and saw that .split() method has a limit parameter, but I am not quite sure how I would set it, if I don't know how many / are within the string. Even when I tried url.split("/",1) instead of url.split("/") I get undefined for my afterLastSlash value.

答案1

得分: 3

首先,你的数组解构是错误的,应该是

let [x, y] = astring.split("/");

如果你想让 xy 包含数组中的前两个标记...

其次,在阅读 String.split() 的文档时,limit 参数限制了结果数组中标记的数量。因此,如果你执行 var [a, b] = astring.split("/", 1),显然 b 将是未定义的,因为 split 的结果只包含一个元素。其余的字符串被忽略...

你可以使用 url.lastIndexOf("/") 来获取最后一个 / 的索引,然后使用 url.substring 来获取该索引之前和之后的字符串:

let url = "https://learn.edu/d2l/login?sessionExpired=1&target=%2fd2l%2fhome";
let i = url.lastIndexOf("/");
if (i >= 0) {
  let beforeLastSlash = url.substring(0, i);
  let afterLastSlash = url.substring(i + 1);
  console.log(beforeLastSlash);
  console.log(afterLastSlash);
}

另一种可能性是使用 / 进行拆分,然后使用 Array.pop 移除结果中的最后一个元素,然后使用 Array.join 将剩余的数组元素重新组合:

let url = "https://learn.edu/d2l/login?sessionExpired=1&target=%2fd2l%2fhome";
let s = url.split("/");
let afterLastSlash = s.pop();
let beforeLastSlash = s.join("/");
console.log(beforeLastSlash);
console.log(afterLastSlash);

以上为代码翻译部分,不包括问题的回答。

英文:

First of all, you array deconstruction is wrong it should be

let [x,y] = astring.split("/")

if you want x and y to contain the first two tokens from the array ...

Second, reading the docs of String.split(), the limit parameter limits the number of tokens in the result array. So if you do var [a,b] = astring.split("/", 1) obviously b will be undefined, because the result of split will only contain 1 element. The rest of the string is ignored ...

You can use url.lastIndexOf("/") to get the index of the last / and then use url.substring to get the string before and after that index

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let url = &quot;https://learn.edu/d2l/login?sessionExpired=1&amp;target=%2fd2l%2fhome&quot;
let i = url.lastIndexOf(&quot;/&quot;);
if (i &gt;= 0) {
  let beforeLastSlash = url.substring(0, i);
  let afterLastSlash = url.substring(i+1);
  console.log(beforeLastSlash);
  console.log(afterLastSlash);
}

<!-- end snippet -->

Another possibility is to split by /, then removing the last element of the result with Array.pop and then put back together the remaining array with Array.join

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let 
  url = &quot;https://learn.edu/d2l/login?sessionExpired=1&amp;target=%2fd2l%2fhome&quot;,
  s = url.split(&quot;/&quot;),
  afterLastSlash = s.pop(),
  beforeLastSlash = s.join(&quot;/&quot;);

console.log(beforeLastSlash);
console.log(afterLastSlash);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月22日 09:57:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定