[英]Given two arrays, how can I iterate over one and push to repeated elements of the second?
I'm writing a google apps script to take in a sheet with rows: 我正在编写一个Google Apps脚本,以接收带有行的工作表:
StudentName, StudentID, Gender, CoursePreference1, CoursePreference2, CoursePreference 3
and outputting: 并输出:
StudentName, StudentID, Gender, CoursePreference1
StudentName, StudentID, Gender, CoursePreference2
StudentName, StudentID, Gender, CoursePreference3
I'm transforming the format so that instead of having one row per student with a column for each of their preferences, I'll have a row for each course preference with repeated student information. 我正在对格式进行转换,以使每个学生都不会在一行中为每个偏好设置一行,而在每个课程中都需要一行并带有重复的学生信息。
I've tried to use other answers with nested for loops and the map method to get to where I'm going, but I'm completely tied in knots. 我尝试过使用嵌套的for循环和map方法的其他答案来到达我要去的地方,但是我完全陷入了困境。
The first half of my code setting up two arrays works fine; 我的代码的前半部分设置了两个数组,效果很好; each element of studentInfo is intended to be repeated for each element of the associated index of courseInfo:
学生信息的每个元素都应针对课程信息的关联索引的每个元素重复:
function requestFileConversion() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var input = ss.getSheetByName('Input');
var output = ss.getSheetByName("Output");
output.getRange('A2:I').clearContent();
output.getRange('A2:I').setNumberFormat('@STRING@');
var studentInfo = input.getRange(2, 1, input.getLastRow() - 1, 8).getValues();
var courseInfo = input.getRange(2, 9, input.getLastRow() - 1, 23).getValues();
var appendValues = [];
then the second half is where I've broken down; 然后下半部分是我分解的地方; my completely non-functional attempt looks like this:
我的完全非功能性尝试如下所示:
for (var i=0; i < courseInfo.length; i++) {
var requestCollection = [];
for (var j=0; j < courseInfo[i].length; j++) {
requestCollection.push(studentInfo[i] + courseInfo[j]);
};
if (requestCollection.length > 0) {
Array.prototype.push.apply(appendValues, requestCollection);
};
};
The desired results of the transformation is to go from a set of rows in the form: 转换的理想结果是从以下形式的一组行中得出:
Jenny, id100, F, English, Science, Math, Physics
Johnny, id101, M, Science, Sports, Gym, English
and produce: 并产生:
Jenny, id100, F, English
Jenny, id100, F, Science
Jenny, id100, F, Math
Jenny, id100, F, Physics
Johnny, id101, M, Science
Johnny, id101, M, Sports
Johnny, id101, M, Gym
Johnny, id101, M, English
You could use destructuring and spread operations and then map to recombine. 您可以使用解构和传播操作,然后进行映射以进行重组。
data = [ ['Jenny', 'id100', 'F', 'English', 'Science', 'Math', 'Physics'], ['Johnny', 'id101', 'M', 'Science', 'Sports', 'Gym', 'English'] ]; result = []; data.map(row => { var [name, id, gender, ...prefs] = row; prefs.map((x) => result.push([name, id, gender, x])); }) console.log(result);
As pointed out in the comments, since some of the features might not be available , here's a more conservative alternative. 正如评论中指出的那样,由于某些功能可能不可用 ,因此这是一个较为保守的选择。
data.forEach(function(row) { prefs = row.slice(3); prefs.map(function(x) { result.push([row[0], row[1], row[2], x]) }); })
Try this: 尝试这个:
function addingRows() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Sheet1');
var rg=sh.getRange(2, 1, sh.getLastRow()-1,sh.getLastColumn());
var vA=rg.getValues();
var vB=[];
for(var i=0;i<vA.length;i++) {
vt=vA[i].slice(3);
for(var j=0;j<vt.length;j++) {
vB.push([vA[i][0],vA[i][1],vA[i][2],vt[j]])
}
}
var osh=ss.getSheetByName('Sheet2');
osh.appendRow(['Name','ID','Gender','Course']);
osh.getRange(2,1,vB.length,4).setValues(vB);
}
My Sheet1: 我的Sheet1:
My Sheet2: 我的Sheet2:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.