【问题标题】:Rebuilding the join array method重建连接数组方法
【发布时间】:2018-10-16 15:58:08
【问题描述】:

需要帮助解决这个问题:

// If the input is empty, return an empty string
    join([]) // ''

// If there is no separator, separate by commas
join([ 'a', 'b', 'c' ]) // 'a,b,c'

// If there is a separator, separate by that string
join([ 'a', 'b', 'c' ], '-') // 'a-b-c'

// If there is only one item, return just that item
join([ 'a' ], '---') // 'a'

下面的代码应该在这里工作:

function join (array, separator) {
  // your code here
}

我有第一个问题的代码:

let result = ""
    if(array.length === 0){
        return ""

【问题讨论】:

标签: javascript arrays function join methods


【解决方案1】:

function join (array, separator) {
  if(array.length === 0) return "";
  if(array.length === 1) return array[0]
  if(typeof separator === 'undefined') return array.join(",");
  return array.join(separator)
}


//this is Just for test 
console.log( join([]))
console.log(join([ 'a', 'b', 'c' ], '-'))
console.log( join([ 'a', 'b', 'c' ]) )
console.log(join([ 'a' ], '---'))

【讨论】:

  • 它并没有真正重建......?您可以使用undefinedseparator 进行直接调用:function join (array, separator) { return array.join(separator); }
  • 标题是"重建连接数组方法" - “重建”部分在哪里?这只是默认 .join() 方法的包装......为什么这是公认的答案? O.o
【解决方案2】:

function join (array, separator = ',') {
  if (!array.length) return '';
  return array.reduce((agg, ele, i) => {
        if (i === 0) {
          agg = ele;
        } else {
          agg = agg + separator + ele;
        }
        return agg;
    }, '');
}

console.log(join([]));
console.log(join(['a', 'b'], '-'));
console.log(join(['a']));

【讨论】:

    【解决方案3】:

    这样的事情应该可以工作:

    function join(array, separator) {
        let result = '';
        if (array.length === 0) {
            return result
        }
    
        if (separator) {
            for (let i = 0; i < array.length; i++) {
                if (i === 0) {
                    result += array[i]
                } else {
                    result += separator + array[i]
                }
            }
        } else {
            for (let i = 0; i < array.length; i++) {
                if (i === 0) {
                    result += array[i]
                } else {
                    result += ',' + array[i]
                }
            }
        }
        return result;
    }
    
    let x = ['a', 'b', 'c']
    let y = []
    
    console.log(join(x, '-'))
    console.log(join(x))
    console.log(join(y))

    【讨论】:

      【解决方案4】:
      let result=[[ 'a','b','c' ],"-"];
      let x="";
      
      
      result[0].forEach(temp => {
          if(result[0]) {
              if (result[0].length === 1) {
                  x=result[0][0];
              }
              else {
                  if (result[1]) {
                      if (x){
                          x=x+result[1]+temp;
                      }
                      else{
                          x=temp
                      }
      
                  }
                  else {
                      if (x){
                          x=x+","+temp;
                      }
                      else{
                          x=temp
                      }
                  }
              }
          }
      
      });
      
      console.log(x)
      

      【讨论】:

        【解决方案5】:
         function join (array, separator) {
            if(!array){
               return null;
           }
           if(array.length === 0){
               return "";
           }
           separator = separator || ',';
        
           var retorno = "";
           for(let i =0; i<array.length; i++){
                retorno  += array[i];
                if( i < array.length-1 ){
                    retorno += separator;
                }
           }
           return retorno;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-20
          • 2011-12-09
          • 1970-01-01
          • 2013-11-12
          相关资源
          最近更新 更多