重建联接数组方法

时间:2018-10-16 15:58:08

标签: javascript arrays function join methods

需要有关此问题的帮助:

// 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 ""

5 个答案:

答案 0 :(得分:0)

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']));

答案 1 :(得分:0)

类似的事情应该起作用:

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))

答案 2 :(得分:0)

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' ], '---'))

答案 3 :(得分:0)

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)

答案 4 :(得分:0)

 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;
}