简体   繁体   English

给定两个数组,如何遍历一个数组并推入第二个数组的重复元素?

[英]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.

相关问题 如何使用一个forEach循环遍历两个不同的数组? - How can I use one forEach loop to iterate over two different arrays? 如何通过循环函数将N个数组的所有数组变量推入一个数组中? - How can I push all array variables of N arrays in just one array over an loop function? 我如何遍历其中的某些TH元素<thead><tr> - how can I iterate over SOME of the TH elements in <thead><tr> 我如何迭代嵌套的部分元素? - How i can iterate over nested section elements? 如何克隆对象并迭代其中一个属性? - How can I clone an object and iterate over one of it's properties? 将给定数组中重复出现的元素和唯一元素分离为两个包含唯一元素和重复元素的新 arrays - Separate the repeated occurring elements and unique elements from a given array to two new arrays containing unique elements and recurring elements javascript同时将两个数组的元素推入一个数组 - javascript simultaneously push elements of two arrays into one array 如何同时映射两个数组? - How can I map over two arrays at the same time? 如何映射两个数组并将匹配作为按钮返回? - How can I map over two arrays and return matches as buttons? 遍历数组并将元素推送到对象 - Iterate over an Array and push elements to an Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM