[英]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'];
Flat Mon
then remove that element from arrayFlat Mon
,则从数组中删除该元素Included Members
then remove that element from arrayIncluded 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?在你的脚本中,如何进行以下修改?
Concat_names.splice(i);
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);
. .
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);
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.