简体   繁体   English

使用 ECMASCRIPT 6 生成器/函数求和 arrays 的最佳方法是什么

[英]What is the best way to sum arrays using ECMASCRIPT 6 Generator/Functions

Is there a better way instead of adding values of arrays up using a generator function as closure?有没有更好的方法而不是使用生成器 function 作为闭包来增加 arrays 的值?

var sumArrays = function(){
  var sum = 0;
  return function*(){
    while(true){
      var array = yield sum;
      if(array.__proto__.constructor === Array){
        sum += array.reduce(function(val,val2){ return val+val2; });
      }
      else sum=0;
    }
  };
};

var gen = sumArrays();
// is this step required to make a generator or could it be done at least differently to spare yourself from doing this step?
gen = gen();

// sum some values of arrays up
console.log('sum: ',gen.next()); // Object { value=0,  done=false}
console.log('sum: ',gen.next([1,2,3,4])); // Object { value=10,  done=false}
console.log('sum: ',gen.next([6,7])); // Object { value=23,  done=false}

// reset values
console.log('sum: ',gen.next(false)); // Object { value=0,  done=false}
console.log('sum: ',gen.next([5])); // Object { value=5,  done=false}

This doesn't seem to be a problem generators are supposed to solve, so I would not use a generator here. 这似乎不是生成器应该解决的问题,所以我不会在这里使用生成器。

Directly using reduce (ES5) seems to be more appropriate: 直接使用reduce (ES5)似乎更合适:

let sum = [1,2,3,4].reduce((sum, x) => sum + x);

As a function: 作为一个功能:

function sum(arr) {
  return arr.reduce((sum, x) => sum + x);
}

If you really want to sum multiple arrays across multiple function calls, then return a normal function: 如果你真的想在多个函数调用中求和多个数组,那么返回一个正常的函数:

function getArraySummation() {
  let total = 0;
  let reducer = (sum, x) => sum + x;
  return arr => total + arr.reduce(reducer);
}

let sum = getArraySummation();
console.log('sum:', sum([1,2,3])); // sum: 6
console.log('sum:', sum([4,5,6])); // sum: 15

Keep it simple. 把事情简单化。

Here is use of for/of loop and arrow function,这里使用for/of循环和箭头function,

const sumArray = myArray => {
    let sum = 0;
    for(const value of myArray)
    sum+= value;
    return sum;
}
const myArray = [1, 2, 5];
console.log(sumArray(myArray)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在课程中使用Ecmascript 6箭头函数作为方法的正确方法是什么? - What is the right way to use Ecmascript 6 arrow functions as methods in classes? 使用 JSDoc 记录 arrays 的 arrays 数组的最佳方法是什么? - What's the best way to document an array of arrays of arrays using JSDoc? 使用 javascript 组合两个 arrays 的最佳方法是什么? - What is the best way to combine two arrays using javascript? 使用angular-fullstack生成器时,在后端访问env变量的最佳方法是什么? - What's the best way to access env variables on the back end when using angular-fullstack generator? 使用 Ecmascript 6 中省略值的默认值初始化对象的最佳方法是什么? - What's the best way to initialize an object with defaults where values are omitted in Ecmascript 6? 新的ECMAScript 5函数有哪些现代化程序脚本? - What modernizer scripts exist for the new ECMAScript 5 functions? 在 Javascript 中处理大型 arrays 的最佳方法是什么? - What is the best way to process large arrays in Javascript? 合并嵌套 arrays 的最佳方法是什么 - What is the best way to merge nested arrays 在蓝鸟中使用“ .then”链接异步功能。 什么是最好的方法? - Chaining async functions in bluebird with “.then”. What is the best way? 在循环中定义函数的最佳方法是什么? -JavaScript - What is the best way for defining functions in loops? - JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM