1

I have a simple array of strings

var myArray= ["This is","a game","worth playing"];

I have tried this

console.log(myArray.map(function(f){ return f.substring(myArray.indexOf(" ") + 1);}));

which ofcourse is not working.

My Expected Output:

is game playing

I need this in pure JavaScript, not jQuery.

5
  • Darnit , no jQuery! Commented Feb 21, 2018 at 2:04
  • 2
    I, on the other hand, would like a pony. Commented Feb 21, 2018 at 2:05
  • @jdv -- Wouldn't we all? Commented Feb 21, 2018 at 2:08
  • 3
    Why wouldn't just changing the last myArray to f work? function(f) { return f.substring( f.indexOf(" ") + 1); } Commented Feb 21, 2018 at 2:15
  • @billjamesdev, Yes, you are spot on. That worked. Thanks. Commented Feb 21, 2018 at 2:22

6 Answers 6

2

I'm not sure why everyone is complicating the situation with their answers... it's a simple case of using indexOf on the wrong variable...

This line:

console.log(myArray.map(function(f){ return f.substring(myArray.indexOf(" ") + 1);}));

Should be:

console.log(myArray.map(function(f){ return f.substring(f.indexOf(" ") + 1);}));

You want the index of the space inside the current string that you are checking, but you were calling indexOf on the original array, which of course returns -1 because the value is not found.

Additionally, if you want the output to be exactly like your expected output statement, you will need to use .join() on the result:

var myArray= ['This is', 'a game', 'worth playing'];
console.log(myArray.map(function(f){ return f.substring(f.indexOf(' ') + 1);}).join(' '));

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

3 Comments

@Herohtar Darnit...! I knew there is a tiny error somewhere. Anyway, thanks so much. Time for a coffee ...
Please look at the question. There is a My Expected Output: section.
That still wasn't what the OP was asking about, and wasn't looking for in an answer, otherwise he wouldn't have selected mine. Nevertheless, I updated the example to produce a string for completeness.
2

You have to look for " " in the array elements, not in tha array itself. Change (myArray.indexOf(" ") to f.indexOf(' ').

var myArray= ["This is","a game","worth playing"];

console.log(myArray.map(f => f.substring(f.indexOf(' ') + 1)).join(' '));

2 Comments

Might want to add an explaination on what you changed so you stop getting dinged.
@epascarello, thanks, I've added the explaination. I thought the code was obvious.
0

The problem is that your .indexOf is returning -1 for each of your spaces in your array. This is because you String.indexOf() and Array.indexOf() are two different methods.

To resolve this, how about simply looping over each of the elements in the array and then dealing with them individually, setting each of them to be equal to second word?

var myArray = ["This is", "a game", "worth playing"];

for (var i = 0; i < myArray.length; i++) {
  myArray[i] = myArray[i].substr(myArray[i].indexOf(" ") + 1);
}

console.log(myArray);
console.log(myArray.join(' '));

2 Comments

Not sure why this was down-voted as it provides a solution to the problem, but given the other answers, someone does indeed seem heavy-handed on the down-vote button ;)
No need to down vote an answer you don't like. Down vote should be used to indicate an answer is invalid not to your liking.
0

You had to save the array processed with map into a variable.

The map() method creates a new array with the results of calling a provided function on every element in the calling array (source).

var myArray= ["This is","a game","worth playing"];
myArray = myArray.map(function(str){ 
  return str.substring(str.indexOf(" ") + 1) 
});
// as array
console.log(myArray);
// as string
console.log(myArray.join(' '));

Comments

0

This one match your expected output:

var myArray= ["This is","a game","worth playing"];
console.log(myArray.map((item) => { return item.replace(/\w+\s*/i, "") }).join(" "))

Or this one without "join":

var myArray= ["This is","a game","worth playing"];
console.log(myArray.map((item) => { return item.replace(/\w+\s*/i, "") }))

Comments

-1
function removeFirstWordFromArrayOfWords(array) {
  return array.map(item => {
    return item.split(' ').slice(1);
  }).join(' ');
}

Which will give you

> var myArray= ["This is","a game","worth playing"];
> removeFirstWordFromArrayOfWords(myArray)
is game playing

This may not be the simplest or fastest

1 Comment

Good shot, but splitUp.length can be omitted here: splitUp.slice(1, splitUp.length)

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.