Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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\u0.reduce()练习_Javascript_Reduce - Fatal编程技术网

Javascript\u0.reduce()练习

Javascript\u0.reduce()练习,javascript,reduce,Javascript,Reduce,请帮忙;我试图解决这个问题: 编写一个函数,该函数接受一组名称并向它们表示祝贺。确保使用u0.reduce作为函数的一部分 var people = ["Steve", "Sally", "George", "Gina"]; people.reduce( function(prev, curr, currIndex, array){ if(currIndex === array.length - 1){ return prev + curr + "!"; } e

请帮忙;我试图解决这个问题:

编写一个函数,该函数接受一组名称并向它们表示祝贺。确保使用u0.reduce作为函数的一部分

var people = ["Steve", "Sally", "George", "Gina"];

people.reduce(
  function(prev, curr, currIndex, array){
    if(currIndex === array.length - 1){
      return prev + curr + "!";
    } else{
      return prev + curr + ", ";
    }
  }, "Congratulations ");
输入:

['Steve', 'Sally', 'George', 'Gina']
输出:

'Congratulations Steve, Sally, George, Gina!'
以下是我到目前为止得到的,但不起作用:

var myArray = _.reduce(['Steve', 'Sally', 'George', 'Gina'], function(current, end) {
return 'Congratulations' + current + end;
});

你可以这样做:

var myArray = 'Congratulations ' + _.reduce(['Steve', 'Sally', 'George', 'Gina'], function(current, end) {
    return current + ', ' + end;
});

// "Congratulations Steve, Sally, George, Gina"
但reduce并不是最方便的工具,简单连接感觉更自然:

'Congratulations ' + ['Steve', 'Sally', 'George', 'Gina'].join(', ');

你可以这样做:

var myArray = 'Congratulations ' + _.reduce(['Steve', 'Sally', 'George', 'Gina'], function(current, end) {
    return current + ', ' + end;
});

// "Congratulations Steve, Sally, George, Gina"
但reduce并不是最方便的工具,简单连接感觉更自然:

'Congratulations ' + ['Steve', 'Sally', 'George', 'Gina'].join(', ');
在这里,您可以完全减少:

1您必须使用祝贺作为第一个元素reducef,请参见

2有两种情况:a未到达最后一个元素,因此追加,b否则追加!。 这是通过检查数组长度i===a.length-1的当前索引来完成的。这里您可以完全减少:

1您必须使用祝贺作为第一个元素reducef,请参见

2有两种情况:a未到达最后一个元素,因此追加,b否则追加!。
这是通过检查数组长度i===a.length-1的当前索引来完成的。这是我的解决方案,它使用reduce函数的所有参数

var people = ["Steve", "Sally", "George", "Gina"];

people.reduce(
  function(prev, curr, currIndex, array){
    if(currIndex === array.length - 1){
      return prev + curr + "!";
    } else{
      return prev + curr + ", ";
    }
  }, "Congratulations ");

这是我的解决方案,它使用reduce函数的所有参数

var people = ["Steve", "Sally", "George", "Gina"];

people.reduce(
  function(prev, curr, currIndex, array){
    if(currIndex === array.length - 1){
      return prev + curr + "!";
    } else{
      return prev + curr + ", ";
    }
  }, "Congratulations ");

为什么要减少工作量,除非你使用的是一个你没有提到的框架,或者你的数组名为。你把它包括进去了吗?为什么要减少工作量,除非你使用的是一个你没有提到的框架,或者你的数组叫做。我想简历是从。你把它包括进去了吗?Javascript 2015等价物:['Steve','Sally','George','Gina']。reduceo,n,i,ar=>i==ar.length-1${o} ,${n}!:${o} ,${n},祝贺JavaScript 2015等价物:[Steve',Sally',George',Gina']${o} ,${n}!:${o} ,${n},恭喜你