【问题标题】:Remove the last character of each string in array (JavaScript)删除数组中每个字符串的最后一个字符 (JavaScript)
【发布时间】:2021-11-23 03:43:21
【问题描述】:

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

【问题讨论】:

    标签: javascript arrays string


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      你可以这样使用

      const array = ['ab', 'aabb', 'ba', 'bbaa'];
      
      const removingLastChar = array.map(string => string.substring(0, string.length - 1));
      
       console.log({ removingLastChar });

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-15
        • 1970-01-01
        • 2017-08-19
        • 1970-01-01
        相关资源
        最近更新 更多