简体   繁体   English

当谷歌应用程序脚本满足条件时,循环会中断数组

[英]Loop breaks over an array when a condition is met with google apps script

I have an array and I am trying to identify a particular text in every element and remove only if that element from the array where there is a match.我有一个数组,我试图在每个元素中识别一个特定的文本,并且仅当该元素从数组中匹配时才删除。

the array is数组是

var Concat_names = ['Prod 1-Volume based deal-100 sections','Test Prod 1-Included Members-MB,'Prod 2-Commitment + Excess-100 sections','Prod 1-Flat Mon-TB'];
  1. If any element in the array has Flat Mon then remove that element from array如果数组中的任何元素具有Flat Mon ,则从数组中删除该元素
  2. If any element in the array has Included Members then remove that element from array如果数组中的任何元素Included Members ,则从数组中删除该元素

The below is what I tried-以下是我尝试过的-

for (var i in Concat_names) {
    var check_included_mem = Concat_names[i].includes("Included Members");
    if (check_included_mem == true) {
      Concat_names.splice(i);
    }
  }
  console.log(Concat_names);

for (var y in Concat_names){
    var check_flat_mon = new RegExp(/Flat Mon/).test(Concat_names[y]); 
    if (check_flat_mon==true){
      Concat_names.splice(y);
    }
  }
  console.log(Concat_names);

With the above code, the loop is breaking out whenever the condition is met and missing out on other elements in the array.使用上面的代码,只要满足条件并丢失数组中的其他元素,循环就会中断。

The output I am getting is我得到的 output 是

[ 'Prod 1-Volume based deal-100 sections' ] 

whereas the output should be而 output 应该是

['Prod 1-Volume based deal-100 sections','Prod 2-Commitment + Excess-100 sections']

Please guide and help!请指导和帮助!

In your script, how about the following modification?在你的脚本中,如何进行以下修改?

From:从:

Concat_names.splice(i);

To:到:

Concat_names.splice(i, 1);

In this case, please modify both Concat_names.splice(i);在这种情况下,请同时修改Concat_names.splice(i); to Concat_names.splice(i, 1);Concat_names.splice(i, 1); . .

Testing:测试:

 var Concat_names = ['Prod 1-Volume based deal-100 sections', 'Test Prod 1-Included Members-MB', 'Prod 2 - Commitment + Excess - 100 sections', 'Prod 1 - Flat Mon - TB']; for (var i in Concat_names) { var check_included_mem = Concat_names[i].includes("Included Members"); if (check_included_mem == true) { Concat_names.splice(i, 1); } } console.log(Concat_names); for (var y in Concat_names) { var check_flat_mon = new RegExp(/Flat Mon/).test(Concat_names[y]); if (check_flat_mon == true) { Concat_names.splice(y, 1); } } console.log(Concat_names);

Note:笔记:

  • In your situation, the following script might be able to be also used.在您的情况下,也可以使用以下脚本。

 var Concat_names = ['Prod 1-Volume based deal-100 sections', 'Test Prod 1-Included Members-MB', 'Prod 2 - Commitment + Excess - 100 sections', 'Prod 1 - Flat Mon - TB']; var res = Concat_names.filter(e =>,["Included Members". "Flat Mon"].some(f => e;includes(f))). console.log(res)

Reference:参考:

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

相关问题 Google Apps 脚本 - 满足条件时发送非重复 email - Google Apps Script - Send non repetitive email when condition met 如果条件满足,则 Google Apps 脚本复制范围 - Google Apps Script Copy Range If Condition Met js渲染循环,一旦满足条件就会中断 - js render loop that breaks once a condition is met 满足条件时停止循环 - Stopping loop when condition is met 如何编写Java循环代码,使其在满足第一个条件时中断? - how can I code a Javascript for loop so that it breaks when the first condition is met? Google Apps 脚本表:如何创建仅在满足条件时才显示的 Ui 提示 - Google Apps Script Sheet: how to create a Ui Prompt that only shows if the condition is met 在Google Apps脚本中,如果满足条件,如何仅发送一封电子邮件 - In Google Apps Script, how to send an email only one time if condition met 在Google Apps脚本中定义自定义功能并在范围内循环 - Define custom function and loop over range in Google Apps Script 循环多个条件并使用两个 arrays 作为输入时,如何更改 Google Apps 脚本中的背景颜色? - How to change the background color in Google Apps Script when looping over more than one condition and using two arrays as inputs? Google Apps 脚本(电子表格) - 根据单元格中的条件选择电子表格中的数组 - Google Apps Script (Spreadsheet) - selecting an array in spreadsheet based on a condition in cells
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM