1

I have an array with strings in which I want to remove the last character. How can I make it possible to remove the last character for all strings that I have in the array?

2 Answers 2

3

use a .map() to get a new array and for each item do a .slice(0,-1) to remove the last char.

const array = ['abcd', '1234', 'cde', 'dot.']
const withoutLastChar = array.map(string => string.slice(0, -1));

console.log(withoutLastChar);

Sign up to request clarification or add additional context in comments.

Comments

0

you can used this way

const array = ['ab', 'aabb', 'ba', 'bbaa'];

const removingLastChar = array.map(string => string.substring(0, string.length - 1));

 console.log({ removingLastChar });

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.