Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 重建联接数组方法_Javascript_Arrays_Function_Join_Methods - Fatal编程技术网

Javascript 重建联接数组方法

Javascript 重建联接数组方法,javascript,arrays,function,join,methods,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' ], '-') // '

需要有关此问题的帮助:

// 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 ""
函数联接(数组,分隔符=','){
如果(!array.length)返回“”;
返回数组.reduce((agg,ele,i)=>{
如果(i==0){
agg=ele;
}否则{
agg=agg+分隔符+ele;
}
返回agg;
}, '');
}
console.log(join([]);
log(join(['a','b'],'-');

log(join(['a'])类似的东西应该可以工作:

函数联接(数组、分隔符){
让结果=“”;
if(array.length==0){
返回结果
}
if(分离器){
for(设i=0;i
函数联接(数组、分隔符){
if(array.length==0)返回“”;
if(array.length==1)返回数组[0]
if(typeof separator==='undefined')返回array.join(“,”);
返回数组.连接(分隔符)
}
//这只是为了测试
console.log(join([]))
log(join(['a','b','c'],'-'))
log(join(['a','b','c']))
log(join(['a'],'--'))
函数join(数组,分隔符){
if(!数组){
返回null;
}
if(array.length==0){
返回“”;
}
分隔符=分隔符| |',';
var=”;

对于(让i=0;iit不是真正重建的…?您可以使用
未定义的
分隔符进行直接调用:
函数join(array,separator){return array.join(separator);}
标题是“重建join数组方法”-“重建”部分在哪里?这只是默认
.join()的包装器
方法…为什么这是公认的答案
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)
 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;
}