【问题标题】:Inserting elements at specific index after regex match in a array in apps scriptInserting elements at specific index after regex match in a array in apps script
【发布时间】:2022-12-02 09:01:08
【问题描述】:

I have a code block which is creating an array where I am searching for certain text using regex and if its found, I am removing that element and inserting two new elements in its place with below code:

var Unique_items = ["P name1-Test based fee-200 Samples", "P name2-night fee-GG", "P name2-day light test-HH" ];
for (var i in Unique_items){
    var temp_index = [];
    var check_test_based_fee = new RegExp(/Test based fee/).test(Unique_items[i]); 

    if (check_test_based_fee==true){
      temp_index.push(i);
      temp_index.push(i+1);
      
      Unique_items.splice(temp_index[0],"P name1-TBF-200 Samples:commitment");
      Unique_items.splice(temp_index[1],"P name1-TBF-200 Samples:Excess");
    }
  }
Logger.log(Unique_items);

So, whenever Test based fee is encountered in any element, it should replace it in the array with two new elements as the Unique_items here to be

["P name1-TBF-200 Samples:commitment", "P name1-TBF-200 Samples:Excess","P name2-night fee-GG", "P name2-day light test-HH"]

I am not getting how to dynamically prepare and insert the

P name1-TBF-200 Samples:commitment", P name1-TBF-200 Samples:Excess

Please help!

【问题讨论】:

  • Please share an example of the desired output. Whenever possible, you need to include a minimal example that reproduces the issue. You can also include the expected behavior, the actual behavior, and how they differ, that would be helpful as well. Please visit How to Ask have some tips on how to write a question, so the community will be able to help you out in a better way.

标签: google-apps-script


【解决方案1】:

I believe your goal is as follows.

  • You want to achieve the following conversion using var Unique_items = ["P name1-Test based fee-200 Samples", "P name2-night fee-GG", "P name2-day light test-HH"]; and "P name1-TBF-200 Samples:commitment" and "P name1-TBF-200 Samples:Excess".

      ["P name1-TBF-200 Samples:commitment", "P name1-TBF-200 Samples:Excess","P name2-night fee-GG", "P name2-day light test-HH"]
    

Modification points:

  • In your script, splice is required to be modified. the arguments of splice is splice(start, deleteCount, item1, item2, itemN).
  • If 2 elements are included in Unique_items, the index of i is changed. It is required to consider this situation.

When these points are reflected in your script, how about the following modification?

Modified script:

var Unique_items = ["P name1-Test based fee-200 Samples", "P name2-night fee-GG", "P name2-day light test-HH"];
for (var i in Unique_items) {
  var check_test_based_fee = Unique_items[i].includes("Test based fee");
  if (check_test_based_fee == true) {
    Unique_items.splice(i, 1, ["P name1-TBF-200 Samples:commitment", "P name1-TBF-200 Samples:Excess"]);
  }
}
Unique_items = Unique_items.flat();
console.log(Unique_items);

Note:

  • In this case, the following sample script might be able to be used.

var Unique_items = ["P name1-Test based fee-200 Samples", "P name2-night fee-GG", "P name2-day light test-HH"];
var res = Unique_items.reduceRight((ar, e) => [...ar, ...(e.includes("Test based fee") ? ["P name1-TBF-200 Samples:Excess", "P name1-TBF-200 Samples:commitment"] : [e])], []).reverse();
console.log(res)

Reference:

【讨论】:

    猜你喜欢
    • 2022-08-27
    • 1970-01-01
    • 2022-12-02
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 2022-12-02
    • 2022-11-09
    相关资源
    最近更新 更多