Home > Software engineering >  Inserting elements at specific index after regex match in a array in apps script
Inserting elements at specific index after regex match in a array in apps script

Time:12-03

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!

CodePudding user response:

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:

  • Related