繁体   English   中英

删除数组中每个字符串的最后一个字符 (JavaScript)

[英]Remove the last character of each string in array (JavaScript)

我有一个包含字符串的数组,我想删除其中的最后一个字符。 我怎样才能删除数组中所有字符串的最后一个字符?

使用.map()获取新数组,并为每个项目执行.slice(0,-1)以删除最后一个字符。

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

你可以这样使用

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM