1

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!

2
  • 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. Commented Dec 1, 2022 at 20:56
  • @Lorena Gomez, the above question did have a reproducible example if you look closely. Commented Dec 2, 2022 at 7:10

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

2 Comments

You are a blessing for humankind, Thank you so much @Tanaike - san!
@Alicia Stone Thank you for replying and testing it. I'm glad your issue was resolved. Thank you, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.