问题
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 |
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.
回答1:
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);
回答2:
Use Array.map
to get the structure you want.
This greatly reduces the number of lines of code.
Idea is to iterate over the database
and for each of the array within add the User A's rating
var database = [
[2, 8, 6],
[4, 6, 0],
[5, 2, 3],
[2, 7, 2]
];
var mainUserRating = database.map(function(v, i) { //v: element; i: index
return v.map(function(ele, index) { // since v itself is an array
return ele + v[0];
});
});
console.log(mainUserRating)
回答3:
The simpler way is to store all the first column elemens into a 1D Array, and then go on adding each element of the 1D array to respective elements of the 2D array.
Pseudo code may look like this :
var firstColumn = [ ]; //which will be storing the only first column elemens
for each row in the table{
Add corresponding element of the firstColumn;
}
来源:https://stackoverflow.com/questions/41657820/sum-array-columns-and-store-the-results-in-a-bidimensional-array-javascript