PAR
PAR

Reputation: 441

Typescript: How remove last item from array

I have an array :

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

After converted into string

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

How to remove last charecter hypen(-) after testing and how should i get new array.

Accepted output

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

Please help me.

Upvotes: 1

Views: 963

Answers (3)

const array = ["one-", "two-", "three-", "testing-"];
let DataStr = array.toString();
let OutputStr = DataStr.replace("-", "");
var res = OutputStr.split(",");
console.log(res);

try this

Upvotes: 0

MattCorr
MattCorr

Reputation: 371

Do this:

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

to take the last character off of your string. Then, to make it into a list:

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

Upvotes: 0

Roland Rácz
Roland Rácz

Reputation: 2999

For an elegant solution use .slice(0, -1) for the string:

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

To get the new array, simply use .split(','):

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

Upvotes: 2

Related Questions