1

I have an array of objects as shown below. I did a console.log(values); and got below result

["15- S&P", "us- ex US", "al- ex CL"]

0:"15- S&P"
1:"us- ex US"
2:"al- ex CL"
length:3
__proto__:Array(0)

I want the output in a array with values as follows.

[S&P, ex US, ex CL]

All values before the '-' are eliminated and values after '-' are taken and put in an array. for e.g. '15- S&P' is changed to 'S&P'. Can anyone please let me know how to achieve this.

2
  • ["15- S&P", "us- ex US", "al- ex CL"].map(s => s.match(/- (.*)/).pop()) Commented Apr 5, 2017 at 21:36
  • Please read How to Ask. Key phrases: "Search, and research" and "Explain ... any difficulties that have prevented you from solving it yourself". Commented Apr 5, 2017 at 21:41

2 Answers 2

3

You could use Array#map and String#split to return just the text after - sign from every element.

var arr = ["15- S&P", "us- ex US", "al- ex CL"],
    res = arr.map(v => v.split("- ")[1]);
    
    console.log(res);

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

Comments

2

You may also try this;

var arr = ["15- S&P", "us- ex US", "al- ex CL"],
    res = arr.map(s => s.replace(/\w+-\s*([\w&\s]+)/,"$1"));
console.log(res);

Comments

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.