【问题标题】:How to convert array into string without comma and separated by space in javascript without using .join()?如何在不使用.join()的情况下将数组转换为不带逗号的字符串并在javascript中用空格分隔?
【发布时间】:2018-07-30 19:57:23
【问题描述】:

我正在尝试寻找 .join() 的替代方法。我想删除“,”并添加一个空格。这是 myArray 所需的输出: 嘿那里

// create a function that takes an array and returns a string 
// can't use join()

// create array
const myArray = ["Hey", "there"];


/**
 * 
 * @param {Array} input
 * @returns {String}
 */

const myArraytoStringFunction = input => {
       // for (var i = 0; i < input.length; i++) {
       //     ????
       // } 
    return input.toString();
};

// call function
const anything1 = console.log(myArraytoStringFunction(myArray));

【问题讨论】:

    标签: javascript arrays tostring


    【解决方案1】:

    您可以使用reduce,如果累加器不为空,则添加一个空格:

    const myArray = ["Hey", "there"];
    const myArraytoStringFunction = input => input.reduce((a, item) => (
      a + (a === '' ? item : ' ' + item)
    ), '');
    console.log(myArraytoStringFunction(myArray));

    【讨论】:

      【解决方案2】:
      const myArraytoStringFunction = function myArraytoStringFunction(input) {
          let r = "";
          input.forEach(function(e) {
              r += " " + e;
          }
          return r.substr(1);
      };
      

      我假设您避免使用join,因为这是家庭作业,所以我没有使用reduce,他们可能还没有使用。

      【讨论】:

        【解决方案3】:

        这是使用递归的替代方法:

        const myArray = ["Hey", "there"];
        const myArraytoStringFunction = inp => 
             (inp[0] || "") + (inp.length>1 ? " " + myArraytoStringFunction(inp.slice(1)) : "");
        const anything1 = console.log(myArraytoStringFunction(myArray));

        【讨论】:

          【解决方案4】:

          您可以通过使用累加器来检查是否将分隔符添加到字符串中。

          const
              array = ["Hey", "there"];
              arrayToString = array => array.reduce((r, s) => r + (r && ' ') + s, '');
          
          console.log(arrayToString(array));

          【讨论】:

            【解决方案5】:
            const myArraytoStringFunction = input => {
                let product = "";
                input.forEach(str => product += " " + str);\
                // original answer above returned this:
                // return product.substr(1); 
                // I used .slice() instead
                return product.slice(1); 
            };
            
            // This was another that I like - Thank you whomever submitted this
            // I did change it a little bit)
            // const myArraytoStringFunction = input => {
            //     let product = "";
            //     input.forEach((str, i) => product += i === 0 ? str : " " + str);
            //     return product;
            // };
            
            
            console.log(myArraytoStringFunction(["I", "think", "it", "works", "now"]));
            console.log(myArraytoStringFunction(["I", "win"]));
            console.log(myArraytoStringFunction(["Thank", "you"]));
            

            【讨论】:

            • 我最终使用了这个。谢谢大家!
            猜你喜欢
            • 2015-03-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-05-21
            • 2011-06-18
            • 2019-05-19
            相关资源
            最近更新 更多