对数组列求和并将结果存储在二维数组中(Javascript) [英] Sum array columns and store the results in a bidimensional array (Javascript)

查看:56
本文介绍了对数组列求和并将结果存储在二维数组中(Javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google表格的脚本编辑器在表/数组的值之间进行操作.我有一张看起来像这样的桌子:

I'm using Google Sheets's Script Editor to operate between the values of a table/array. I've got a table that looks like this:

       | UserA | UserB | UserC |
Movie1 |   2   |   8   |   6   |
Movie2 |   4   |   6   |   0   |
Movie3 |   5   |   2   |   3   |
Movie4 |   2   |   7   |   2   |

我需要将每个用户的评分与第一个用户的评分相加,以获取每个结果的另一个数组.

I need to sum every user's rating with the first user's rating, obtaining another array with the results of each sum.

在这种情况下,结果应为:

In this case, the result should be:

       | UserA+A | UserB+A | UserC+A |
Movie1 |    4    |   10    |    8    |
Movie2 |    8    |   10    |    4    |
Movie3 |   10    |    7    |    8    |
Movie4 |    4    |    9    |    4    |

我正在使用两个"for"循环遍历所有列和行,但是我无法设法将结果存储回二维数组.这就是我所拥有的:

I'm running through all columns and rows with two "for" loops, but I can't manage to store the results back to a bidimensional array. This is what I've got:

var userRatings = [];
var mainUserRatings = [];
var ultraArray = [];    

for (i = 0; i < database.length; i++) { // Run through all movies (rows)
   var mainUserRating = database[i][1]; // Ratings from UserA
   mainUserRatings.push(mainUserRating);

   for (n = 1; n < database[0].length; n++) { // Run through all users (columns)
      var userRating = database[i][n]; // Ratings from User "n"
      userRatings[i] = userRating;
   }

   ultraArray[i] = userRatings;
}
return ultraArray;

此函数返回一个数组,该数组具有与第一个表一样多的行和列(只有它们被切换了),并且只有最后一列的数据才在所有行中重复.简而言之:这是一场灾难.而且我还没有达到将数据与第一列相加的地步...

This function returns an array that has as many rows and columns as the first table (only they're switched) and only the data from the last column is repeated through all the rows. In short: it's a disaster. And I haven't even got to the point where I sum the data with the first column...

我知道我将数据推入新数组的方式有问题,可能还涉及循环逻辑,但是我找不到正确的组合.任何帮助将不胜感激.

I know there's something wrong about the way I'm pushing the data into the new array, and probably also on the logic of the loops, but I fail to find the right combination. Any help would be much appreciated.

推荐答案

希望我已经正确解释了您的问题,但这是一个示例,您可以如何完成对第一个用户的评分与其余用户评分的总和数据库.假设您有一个二维的收视率数组,其中索引i的数据库表示特定电影的收视率,则可以遍历所有收视率并添加第一个收视率的结果,如下所示.希望这会有所帮助!

Hopefully I've interpreted your question correctly, but here is an example of how you could accomplish summing the first user's rating with the rest of the user ratings in the database. Assuming you have a two dimensional array of ratings, where the database at index i represents ratings for a particular movie, you can iterate through all of the ratings and add the result of the first rating as shown below. Hope this helps!

var database = [ [2, 8, 6], [4, 6, 0], [5, 2, 3], [2,7,2] ];
var newRatings = [ ];
var firstUserRating;
var ratingSums;
var i, j;


for (i = 0; i < database.length; i++) { // Run through all movies (rows)
   firstUserRating = database[i][0]; // Get the first user's rating.
   ratingSums = [];
   
   // Run through the ratings for the current movie (database[i])
   for (j = 0; j < database[i].length; j++) {
     // Return the original value + firstUserRating.
     ratingSums.push(firstUserRating + database[i][j]);
   }
  
   newRatings.push(ratingSums);
}
console.log(newRatings);

这篇关于对数组列求和并将结果存储在二维数组中(Javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆