1

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.

3 Answers 3

2

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"]
Sign up to request clarification or add additional context in comments.

Comments

0

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(“,”);

Comments

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

try this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.