简体   繁体   English

从字符串数组中删除第一个“单词”

[英]Remove first “word” from array of strings

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. 我需要纯JavaScript,而不是jQuery。

You have to look for " " in the array elements, not in tha array itself. 你必须在数组元素中寻找" " ,而不是在数组本身中。 Change ( myArray.indexOf(" ") to f.indexOf(' ') . 将( myArray.indexOf(" ")更改为f.indexOf(' ')

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

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... 我不确定为什么每个人都用他们的答案使情况变得复杂......这是在错误的变量上使用indexOf的一个简单例子......

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. 您想要检查当前字符串中的空间索引,但是您在原始数组上调用indexOf ,当然返回-1因为找不到该值。

Additionally, if you want the output to be exactly like your expected output statement, you will need to use .join() on the result: 此外,如果您希望输出与预期的输出语句完全相同,则需要在结果上使用.join()

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

The problem is that your .indexOf is returning -1 for each of your spaces in your array. 问题是你的.indexOf为你的数组中的每个空格返回-1 This is because you String.indexOf() and Array.indexOf() are two different methods. 这是因为String.indexOf()Array.indexOf()是两种不同的方法。

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(' ')); 

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, "") })) 

You had to save the array processed with map into a variable. 您必须将使用map处理的数组保存到变量中。

The map() method creates a new array with the results of calling a provided function on every element in the calling array ( source ). map()方法创建一个新数组,其结果是在调用数组( )中的每个元素上调用提供的函数。

 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(' ')); 

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 这可能不是最简单或最快的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM