I have an Array:
arr1 = ["gbt100", "gbt1130", "gbt12300", "gbt104230"]
How can I remove the "gbt" string from each of the elements?
I have an Array:
arr1 = ["gbt100", "gbt1130", "gbt12300", "gbt104230"]
How can I remove the "gbt" string from each of the elements?
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, ""));
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)