javascript 使用 underscore.js 将两个(或多个)数组映射为一个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15108250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:31:45  来源:igfitidea点击:

Mapping two (or more) arrays into one with underscore.js

javascriptunderscore.js

提问by David Pardo

I'd need to add element-wise several arrays. That is, I have several arrays of equal lenght, and I'd need just one with the same number of elements that are the sum of the inputs. Underscore has methods to fold all elements into one and to map every element using a function, but I can't find any way to combine two arrays piece wise.

我需要按元素添加几个数组。也就是说,我有几个等长的数组,我只需要一个元素数量与输入总和相同的数组。Underscore 具有将所有元素折叠为一个并使用函数映射每个元素的方法,但我找不到任何方法将两个数组分段组合。

If my original arrays were [1,2,3,4,5,6], [1,1,1,1,1,1]and [2,2,2,2,2,2]the result should be [4,5,6,7,8,9].

如果我原来的数组是[1,2,3,4,5,6][1,1,1,1,1,1][2,2,2,2,2,2]结果应该是[4,5,6,7,8,9]

I know I can do it by iterating over the arrays, but wonder if it would be easier/faster using underscore.js functions. Can I do it? How?

我知道我可以通过迭代数组来做到这一点,但想知道使用 underscore.js 函数是否会更容易/更快。我可以做吗?如何?

回答by Bergi

Easier yes, faster no. To emulate a zipWith, you can combine a zipwith a sum-reduce:

更容易是,更快不是。要模拟 a zipWith,您可以将 azip与 sum-结合使用reduce

var arrays = [[1,2,3,4,5,6], [1,1,1,1,1,1], [2,2,2,2,2,2]];

_.map(_.zip.apply(_, arrays), function(pieces) {
     return _.reduce(pieces, function(m, p) {return m+p;}, 0);
});

回答by elclanrs

I don't think it would be easier with underscore. Here's two options:

我认为下划线不会更容易。这里有两个选项:

var a = [1,2,3,4,5,6]
  , b = [1,1,1,1,1,1]
  , c = [2,2,2,2,2,2];

var result = a.map(function(_,i) {
  return a[i] + b[i] + c[i];
});

// OR

var result = [];
for (var i = 0; i < a.length; i++) {
  result.push(a[i] + b[i] + c[i]);
}

console.log(result); //=> [4,5,6,7,8,9]

回答by Daniel Margol

You could use lodash (https://lodash.com/) instead of underscore which has a pretty cool zipWith (https://lodash.com/docs#zipWith) operator that would work like the example below. (note _.add is also a lodash math function)

您可以使用 lodash ( https://lodash.com/) 而不是下划线,它有一个非常酷的 zipWith ( https://lodash.com/docs#zipWith) 操作符,可以像下面的例子一样工作。(注意 _.add 也是一个 lodash 数学函数)

var a = [1,2,3,4,5,6]
  , b = [1,1,1,1,1,1]
  , c = [2,2,2,2,2,2];

var result = _.zipWith(a, b, c, _.add);

// result = [4, 5, 6, 7, 8, 9]

回答by Penny Liu

Using Lodash 4:

使用Lodash 4

var arrays = [
  [1, 2, 3, 4, 5, 6],
  [1, 1, 1, 1, 1, 1],
  [2, 2, 2, 2, 2, 2]
];

var result = _.map(_.unzip(arrays), _.sum);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

回答by benzonico

You can use zip in conjunction with map and reduce: Not tested but something like this might work

您可以将 zip 与 map 和 reduce 结合使用:未经测试,但类似这样的方法可能有效

var result = _.map(_.zip(array1, array2, array3),function(zipped){return _.reduce(zipped,  function(memo, num){ return memo + num; }, 0)});