Dilip G Dilip G - 17 days ago 5
Javascript Question

Remove all '+' from array

I have an array like this, and I want to remove the

+
sign from the below array:

var arr = ['+(91)-80-411311015', '+(91)-80-411311456', '+(91)-80-411311016'];


I have tried the following but it is not working:

var toRemove = "+";
arr = arr.filter(function(el){
return !toRemove.includes(el);
});


How do I remove all
+
signs from the array?

Answer

Use Array#map method with String#replace method. Where String#replace can be use for replacing + symbol from the string.

var arr = ['+(91)-80-411311015', '+(91)-80-411311456', '+(91)-80-411311016'];

var toRemove = '+'; // use regex `/\+/g` for removing all occurrence

arr = arr.map(function(el) {
  return el.replace(toRemove, '');
});

console.log(arr);


Or simple for loop is enough.

var arr = ['+(91)-80-411311015', '+(91)-80-411311456', '+(91)-80-411311016'];

var toRemove = '+'; // use regex `/\+/g` for removing all occurrence

for (var i = 0; i < arr.length; i++) {
  arr[i] = arr[i].replace(toRemove, '');
}

console.log(arr);