-1

I have an Array:

arr1 = ["gbt100", "gbt1130", "gbt12300", "gbt104230"]

How can I remove the "gbt" string from each of the elements?

Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
PAyTEK
  • 57
  • 7

2 Answers2

8

You can use the map method by passing a callback function as an argument which is applied for every item from your given array.

Also, you need to use replace method in order to remove the gbt string.

arr1 = ["gbt100", "gbt1130", "gbt12300", "gbt104230"]
arr1 = arr1.map(elem => elem.replace("gbt", ""));
console.log(arr1);

Another approach is to pass a regex as the first argument for the replace method.

arr1 = ["gbt100", "gbt1130", "gbt12300", "gbt104230"]
arr1 = arr1.map(elem => elem.replace(/gbt/g, ""));
console.log(arr1);

If you want to remove all the alphabetical chars just change the regex expression inside replace method.

arr1 = arr1.map(elem => elem.replace(/[a-zA-Z]/g, ""));
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
-1

You can try Array.Map and for each of the item using substring

const arr1 = ["gbt100", "gbt1130", "gbt12300", "gbt104230"]

const newArr = arr1.map(x=>x.substring(3, x.length))

console.log(newArr)
Isaac
  • 12,042
  • 16
  • 52
  • 116