Home > Enterprise >  Remove the last character of each string in array (JavaScript)
Remove the last character of each string in array (JavaScript)

Time:10-01

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?

CodePudding user response:

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);

CodePudding user response:

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 });

  • Related