[英]javascript add multidimensional array values to new array
I have an multidimensional array like 我有一个像多维数组
var arr = [[1,2,3],[1,3,2],[1,1,1]];
and I would like to add up what would be a matrix column for each column into a new array. 我想将每列的矩阵列加到一个新数组中。 The result I am expect would be [3,6,6]. 我预期的结果将是[3,6,6]。 This is the solution I came up with 这是我想出的解决方案
function compute(arr) {
var X = _.reduce(arr, function(acc, j) {
acc += j[0];
return acc;
},0);
var Y = _.reduce(arr, function(acc, k) {
acc += k[1];
return acc;
},0);
var Z = _.reduce(arr, function(acc, l) {
acc += l[2];
return acc;
},0);
return [X,Y,Z];
}
It is working but seems redudent to have 3 reduce functions. 它正在工作,但似乎具有3个reduce函数。 I am looking for a cleaner, more functional, faster solution using lodash. 我正在寻找使用lodash的更干净,更实用,更快的解决方案。
I was thinking maybe I could use the _.flatten function since you can pass it a function for each iteration but I can't seem to avoid all the looping. 我在想也许可以使用_.flatten函数,因为您可以在每次迭代时将其传递给函数,但是我似乎无法避免所有循环。 Any suggestions? 有什么建议么?
You can declare a function inside your function that handles your index and your array: 您可以在函数内部声明一个处理索引和数组的函数:
function compute(arr) {
var reduce = function (index) {
return _.reduce(arr, function(acc, array) {
acc += array[index];
return acc;
},0);
}
var X = reduce(0);
var Y = reduce(1);
var Z = reduce(2);
return [X,Y,Z];
};
If you want to make it a bit shorter, you could also just to this: 如果您想使其更短一些,也可以这样做:
function compute(arr) {
var reduce = function (index) {
return _.reduce(arr, function(acc, array) {
acc += array[index];
return acc;
},0);
}
return [reduce(0), reduce(1), reduce(2)];
};
If you want to make it a even shorter, you could also just to this. 如果您想使其更短,也可以做到这一点。 This one is a bit more functional because you're passing in the function as an argument to _.map
此功能更具功能性,因为您要将函数作为_.map
的参数_.map
function compute(arr) {
var reduce = function (index) {
return _.reduce(arr, function(acc, array) {
acc += array[index];
return acc;
},0);
}
return _.map([0, 1, 2], reduce);
};
You can check out the result here: http://jsfiddle.net/thejsj/p33hmc8q/ 您可以在此处查看结果: http : //jsfiddle.net/thejsj/p33hmc8q/
a simple way using vanilla to avoid an inner anon: 使用香草避免内部负离子的简单方法:
var arr = [[1,2,3],[1,3,2],[1,1,1]];
function sum(a,b){return a+b;}
function pluck(a){return a[this];}
arr.map(function(a,b,c){
return c.map(pluck,b).reduce(sum);
}); // == [3, 6, 6]
the same in underscore/lodash: 下划线/破折号相同:
var arr = [[1,2,3],[1,3,2],[1,1,1]];
function sum(a,b){return a+b;}
_.map(arr, function(a,b,c){
return _.reduce(_.pluck(c, b), sum);
}); // == [3, 6, 6]
personally, i like the native chaining of the Array methods, so i don't have to balance parens, but either way works. 就个人而言,我喜欢Array方法的本机链接,因此我不必保持平衡,但是无论哪种方法都有效。
it nicer to use different low-level interchangeable parts than one big procedure so that you can reduce, re-use, and recycle your keystrokes as you find yourself needing to do almost the same thing again later. 与使用一个大程序相比,使用不同的低级可互换零件更好,这样您就可以减少,重复使用和回收击键,因为您发现自己以后需要再次做几乎相同的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.