[英]Remove the last character of each string in array (JavaScript)
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?
我怎样才能删除数组中所有字符串的最后一个字符?
use a .map()
to get a new array and for each item do a .slice(0,-1)
to remove the last char.使用
.map()
获取新数组,并为每个项目执行.slice(0,-1)
以删除最后一个字符。
const array = ['abcd', '1234', 'cde', 'dot.'] const withoutLastChar = array.map(string => string.slice(0, -1)); console.log(withoutLastChar);
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 });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.