【问题标题】:How to convert arraylist to string in Javascript?如何在Javascript中将arraylist转换为字符串?
【发布时间】:2015-11-30 03:02:26
【问题描述】:

如何在 JavaScript 中将数组列表转换为字符串?

我只想在 Javascript 中将 arraylist 转换为字符串。 我有这个代码。

var digitStack = [];
digitStack.push(1);
digitStack.push(2);
digitStack.push(3);

我想制作“123”。 你有什么好的想法吗?

【问题讨论】:

    标签: javascript jquery html arrays


    【解决方案1】:

    用户数组名.join();

    var digitStack = [];
    digitStack.push(1);
    digitStack.push(2);
    digitStack.push(3);
    
    var digitStr = digitStack.join(''); //predefined function
    
    document.write(digitStr); // just to verify the string, it has to be removed later
    

    【讨论】:

      【解决方案2】:

      只需使用Array.join()

      var digitStack = [];
      digitStack.push(1);
      digitStack.push(2);
      digitStack.push(3);
      
      var digitStr = digitStack.join('');
      
      console.log(digitStr); // gives you a string.

      【讨论】:

        【解决方案3】:

        您可以使用带有空字符串的Array.join() 作为分隔符

        var digitStack = [];
        digitStack.push(1);
        digitStack.push(2);
        digitStack.push(3);
        var string = digitStack.join('');
        
        snippet.log(string)
        <!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
        <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

        【讨论】:

          猜你喜欢
          • 2011-11-12
          • 1970-01-01
          • 2019-09-04
          • 2014-03-06
          • 2016-09-12
          • 2012-01-20
          • 2012-04-28
          • 2012-02-14
          • 1970-01-01
          相关资源
          最近更新 更多