user5471528 user5471528 - 7 months ago 10
Javascript Question

How do I get an array of substrings from an array of strings?

I have an array of string like:

var myArray=['rwt-cable1','rwt-cable42','rwt-cable40',...]


But what I am really interested in is:

['cable1','cable42','cable40',...]


What would be the best way? I'm currently looping through items and extract substrings to get my output array.

Answer

Simpler would be

var myArray=['rwt-cable1','rwt-cable42','rwt-cable40']
myArray.map(x=>x.replace('rwt-',''));
//["cable1", "cable42", "cable40"]
Comments