[英]how to remove everything after a particular string from an array of strings in javascript
I have an array of strings from which I want to remove anything after with
in the statement.我有一个字符串数组,我想在语句中的
with
之后从中删除任何内容。 For example, let's take a string例如,让我们取一个字符串
var str = ['hey what is with you guys',
'you mean with out',
'hello no with is you']
the new str2 should return新的 str2 应该返回
['hey what is',
'you mean' ,
'hello no'
]
I have written the following code but it is not giving the desirable output.我已经编写了以下代码,但它没有给出理想的 output。 My code:
我的代码:
function newArray(str){
for (var i = 0; i <= (str.length - 1); i++){
var k = str[i].toString()
j = k.split('with')
var arr2 = []
var newArr = arr2.concat(j[0])
console.log(newArr)
}
return newArr
}
var str = ['hey what is with you guys',
'you mean with out',
'hello no with is you']
newArray(str)
the output is output 是
[ 'hey what is ' ]
[ 'you mean ' ]
[ 'hello no ' ]
However, I want my output但是,我想要我的 output
['hey what is',
'you mean' ,
'hello no'
]
I do not see any reason to split the strings into arrays and then join them back into strings;我看不出有任何理由将字符串拆分为 arrays 然后将它们重新加入字符串; you could manage everything using methods of the String object:
您可以使用 String object 的方法管理所有内容:
function newStrings(arr, str) { return arr.map(string => { const index = string.indexOf(str); return index > 0? string.substr(0, index).trim(): string; }) } //test const original = [ 'hey what is with you guys', 'you mean with out', 'hello no with is you' ]; const modified = newStrings(original, 'with') console.log('original:', original); console.log('modified', modified);
function newStrings(arr, str) { var output = []; for (var i = 0; i < arr.length; i++) { var myString = arr[i]; var index = myString.indexOf(str); var outStr = index > 0? myString.substr(0, index).trim(): myString; output.push(outStr); } return output } //test var original = [ 'hey what is with you guys', 'you mean with out', 'hello no with is you' ]; var modified = newStrings(original, 'with') console.log('original:', original); console.log('modified', modified);
Here is a solution proposal, using ES6 Features such as Array.map
这里有一个解决方案建议,使用 ES6 特性,例如
Array.map
const str = [
'hey what is with you guys',
'you mean with out',
'hello no with is you',
];
function removeWith(arr) {
// Creates an array from an input array
return arr.map((line) => {
// For each line, split the string with the word "with"
// I use a Regex with the `insensitive` flag to make it case insensitive.
// So it works with "with" and "With" and "wiTH" ...
return line.split(/with/i)[0];
// [0] will return the first part of the string, even if nothing was split.
});
}
you can try this and it worked.你可以试试这个,它奏效了。
var str = ['hey what is with you guys', 'you mean with out', 'hello no with is you'] let arr2 = [] function newArray(str){ for (var i = 0; i <= (str.length - 1); i++){ var k = str[i].toString() let j = k.split('with') arr2.push(j[0]) } return arr2; } let ans = newArray(str); console.log(ans)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.