简体   繁体   English

将多维数组的特定值加在一起

[英]Add specific values of multidimensional array together Javascript

Okay, so I have a multidimensional array that itself contains 9 arrays. 好的,所以我有一个多维数组,该数组本身包含9个数组。 Each of these nested arrays contains 10 numeric values. 这些嵌套数组中的每一个包含10个数字值。 For sake of simplicity, let's say it all looks like this: 为了简单起见,我们假设一切都像这样:

var MyArray = [
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10]
]

I am trying to write a function that will take the first index of each nested array (in this case, all 1's) and add them together, pushing this value either to an array or an object. 我正在尝试编写一个函数,该函数将获取每个嵌套数组的第一个索引(在本例中为全1)并将它们加在一起,然后将此值推入数组或对象。 Then, I need this function to continue on, adding all the values of the next index, and the next, and so on and so forth. 然后,我需要继续执行此功能,添加下一个索引和下一个索引的所有值,依此类推。 In the end, I should have an array of 10 values (or an object works here as well). 最后,我应该有10个值的数组(或者一个对象也可以在这里工作)。 The values would be: 值将是:

1+1+1+1+1+1+1+1+1,
2+2+2+2+2+2+2+2+2,
3+3+3+3+3+3+3+3+3...

...and so on so forth, so that the actual values of the new array would be this: ...依此类推,以便新数组的实际值为:

[9, 18, 27, 36, 45, 54, 63, 72, 81]

The catch here is that I need this to by flexible/dynamic, so that it will work in case MyArray has only 6 arrays, or maybe the nested arrays have only 4 values each. 这里的问题是我需要灵活/动态地使用它,以便在MyArray只有6个数组或者嵌套数组每个只有4个值的情况下工作。 It should work with any amount of nested arrays, each with their own amount of values (though each nested array will contain the SAME amount of values as one another!). 它可以与任意数量的嵌套数组一起使用,每个嵌套数组都有自己的值数量(尽管每个嵌套数组都将包含相同数量的值!)。

What would be the best way to accomplish this via JavaScript and/or jQuery? 通过JavaScript和/或jQuery完成此操作的最佳方法是什么? Note that I could also have the values output to an object, in this fashion: 请注意,我还可以通过以下方式将值输出到对象:

{1:9, 2:18, 3:27, 4:36, 5:45, 6:54, 7:63, 8:72, 9:81}

I tried using similar code to this from another StackOverflow thread to get an object, but it is returning 我尝试从另一个StackOverflow线程使用与此类似的代码来获取对象,但它正在返回

{1:NaN, 2:NaN, 3:NaN, etc.} 

That thread can be found here: 该线程可以在这里找到:

Javascript Multidimensional Array: Add Values Javascript多维数组:添加值

I'm using the "underscore" method and the jQuery $.each part of it provided by Otto. 我使用的是“下划线”方法以及由Otto提供的jQuery $ .each部分。

Anyone able to help here?? 有人可以在这里帮忙吗?

Something like this 像这样

 var myData = [
     [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
     [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
     [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
     [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
     [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
     [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
     [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
     [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
     [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
 ];

 var summed = [];

 myData[0].forEach(function (arr, index) {
     var sum = myData.reduce(function (a, b) {
         return a + b[index];
     }, 0);

     summed.push(sum);
 });

 console.log(summed);

On jsfiddle jsfiddle上

A simple array solution would be the following : 一个简单的数组解决方案如下:

var results= [];

for (var i=0;i<MyArray.length;i++) {
    for(var j=0; j<MyArray[i].length; j++) {
        if(results[j] == undefined) { results[j] = 0; }
          results[j] = results[j]+data[i][j];

    }
}

Note the if(results[j]==undefined) line -- this is probably what you didn't do. 注意if(results[j]==undefined)行-这可能是您没有做的。 If you omit that, you get NaN on all lines, since you're adding an undefined value to a number. 如果您忽略了这一点,则由于在数字上添加了未定义的值,因此所有行均显示NaN

Here is another solution: 这是另一种解决方案:

var MyArray = [
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10]
]

var results= [];

MyArray.map(function(a){
    for(var i=0;i<a.length;i++){
        if(results.length === a.length){
            results[i] = results[i] + a[i];
        }else{
            results.push(a[i]);
        }
    }
});

http://jsfiddle.net/uMPAA/ http://jsfiddle.net/uMPAA/

Another approach to sum columns in multi-dimensional arrays (based on Lodash 4 ). 对多维数组中的列求和的另一种方法(基于Lodash 4 )。

 var arrays = [ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ]; function sum_col(arrays) { return _.map(_.unzip(arrays), _.sum); } console.log(sum_col(arrays)); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM