1

Given this input array:

var data = ["[email protected]","[email protected]","[email protected]"];

I to output an array that looks like this:

["ramu","ravi","karthik"];

I have tried this, but it doesn't produce the correct result:

names = data.map(s => s.split('@'));
6
  • 2
    What have you tried? Where are you running into problems? Stack Overflow is not a free code-writing service. Please add an minimal reproducible example of your attempt. Commented Nov 16, 2017 at 8:51
  • Sure Cerbrus here after i will do Commented Nov 16, 2017 at 9:07
  • 2
    Also, partially duplicate of: How can I extract the user name from an email address using javascript? Commented Nov 16, 2017 at 9:10
  • I have edited the question please remove from hold @Cerbrus Commented Nov 16, 2017 at 9:24
  • As you can see, I'm not the only one that close-voted. If that is what you tried, why didn't you add it in the first place? Or did you just get that from an answer? Commented Nov 16, 2017 at 9:29

4 Answers 4

1

You need to use map, that will go thru each item. In each item, use split() to split the data w.r.t @. After split(), you would get an array. You need to return 0th element of that array:

var data = ["[email protected]","[email protected]","[email protected]"];

var getNames = data.map(function(item){
  return item.split('@')[0];
});

console.log(getNames);

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

7 Comments

Why the down-vote? I see that everyone's answer has been downvoted. Why?
I dont know who is down voted, just now i up the vote
Maybe we don't need 5 answers on something as trivial as splitting a string. Especially considering the lack of shown effort in the question.
@Cerbrus, it's more like a bulb change ... ;-)
May be. But every answer doesn't have a detailed explanation on why map or split has been used. And irrespective of that, efforts put by someone on getting the right answer should not be down-voted.
|
0

Use array map:

var data = ["[email protected]","[email protected]","[email protected]"];
var newData = data.map(s => s.split('@')[0]);
console.log(newData);

Comments

0

You don't need regex for this . This can be done in a simpler way

var data = ["[email protected]","[email protected]","[email protected]"];

var  finalData = data.map(function(email){
    return email.split('@')[0]

}

Comments

0

below is the solution

var names = data.map(email => email.split("@")[0] )

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.