打字稿:如何从数组中删除最后一项

时间:2018-04-04 14:28:24

标签: arrays angular typescript

我有一个数组:

["one-", "two-", "three-", "testing-"]

转换成字符串后

"one-,two-,three-,testing-"

如何在测试后删除最后一个charecter hypen( - )以及如何获得新数组。

接受的输出

["one-", "two-", "three-", "testing"]

请帮帮我。

3 个答案:

答案 0 :(得分:2)

对于优雅的解决方案,请使用.slice(0, -1)作为字符串:

let newString = "one-,two-,three-,testing-".slice(0, -1); // "one-,two-,three-,testing"

要获取新数组,只需使用.split(',')

let newArray = newString.split(','); // ["one-", "two-", "three-", "testing"]

答案 1 :(得分:0)

这样做:

var str = "one-,two-,three-,testing-";
str = str.substring(0, str.length - 1);

从字符串中取出最后一个字符。然后,将其列入一个列表:

var newList = str.split(“,”);

答案 2 :(得分:0)

Unhandled exception
Traceback (most recent call last):
   File "/usr/local/lib/python3.6/site-packages/aiohttp/web_protocol.py", line 428, in start
     await payload.readany()
   File "/usr/local/lib/python3.6/site-packages/aiohttp/streams.py", line 325, in readany
     raise self._exception
   File "/web_api/app/views/resources/Uploader.py", line 49, in post
     json_body = await self.request.json()
   File "/usr/local/lib/python3.6/site-packages/aiohttp/web_request.py", line 512, in json
     body = await self.text()
   File "/usr/local/lib/python3.6/site-packages/aiohttp/web_request.py", line 506, in text
     bytes_body = await self.read()
   File "/usr/local/lib/python3.6/site-packages/aiohttp/web_request.py", line 494, in read
     chunk = await self._payload.readany()
   File "/usr/local/lib/python3.6/site-packages/aiohttp/streams.py", line 325, in readany
     raise self._exception
 aiohttp.web_protocol.RequestPayloadError: 400, message='Can not decode content-encoding: gzip'

试试这个