Javascript - 将修剪函数应用于数组中的每个字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19293997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:02:26  来源:igfitidea点击:

Javascript - Apply trim function to each string in an array

javascriptfunction

提问by neuront

Want to trim each string in an array, e.g., given

想要修剪数组中的每个字符串,例如,给定

x = [' aa ', ' bb '];

output

输出

['aa', 'bb']

My first trial is

我的第一次尝试是

x.map(String.prototype.trim.apply)

It got "TypeError: Function.prototype.apply was called on undefined, which is a undefined and not a function" in chromium.

它在铬中得到“TypeError:Function.prototype.apply was called on undefined, which is an undefined and not a function”。

Then I tried

然后我试过了

x.map(function(s) { return String.prototype.trim.apply(s); });

It works. What's the difference?

有用。有什么不同?

回答by Bergi

String.prototype.trim.applyis the Function.prototype.applymethodwithout being bound to trim. mapwill invoke it with the string, the index and the array as arguments and nothing (undefined) for the thisArg- however, applyexpects to be called on functions:

String.prototype.trim.apply是不受约束的Function.prototype.apply方法trimmap将使用字符串、索引和数组作为参数调用它,Arg没有任何内容 ( undefined) - 但是,期望在函数上调用:thisapply

var apply = String.prototype.trim.apply;
apply.call(undefined, x[0], 0, x) // TypeError

What you can do is passing the trimfunction as the context for call:

您可以做的是将trim函数作为上下文传递给call

[' aa ', ' bb '].map(Function.prototype.call, String.prototype.trim)
// ['aa', 'bb']

What happens here is

这里发生的事情是

var call = Function.prototype.call,
    trim = String.prototype.trim;
call.call(trim, x[0], 0, x) ≡
      trim.call(x[0], 0, x) ≡
            x[0].trim(0, x); // the arguments don't matter to trim

回答by Dominic

Or this can be solved with arrow functions:

或者这可以用箭头函数解决:

x.map(s => s.trim());

回答by Sachin Kainth

If you are using JQuery, then a better way to do this, as it will work with IE8 as well (I need to support IE8) is this:

如果您使用的是 JQuery,那么更好的方法是这样做,因为它也适用于 IE8(我需要支持 IE8)是这样的:

$.map([' aa ', ' bb ', '   cc '], $.trim);

回答by musemind

The simple variant without dependencies:

没有依赖关系的简单变体:

 for (var i = 0; i < array.length; i++) {
     array[i] = array[i].trim()
 }

ES6 variant:

ES6 变体:

const newArray = oldArray.map(string => string.trim())

ES6 function variant:

ES6 函数变体:

const trimArray = array => array.map(string => string.trim())

回答by Denys Séguret

First, do it simply :

首先,简单地做:

x.map(function(s) { return s.trim() });

Then, the reason why the first one doesn't work is that the string is passed as argument to the callback, not as context. As you pass no argument to apply, you get the same message you would have got with

然后,第一个不起作用的原因是该字符串作为参数传递给回调,而不是作为上下文。当您不向 传递任何参数时apply,您将获得与使用相同的消息

var f = String.prototype.trim.apply; f.call();


Now, mostly for fun, let's suppose you're not happy with the fact that mapuse the callback this way and you'd want to be able to pass a function using the context, not the argument.

现在,主要是为了好玩,让我们假设您对以map这种方式使用回调不满意,并且您希望能够使用上下文而不是参数传递函数。

Then you could do this :

那么你可以这样做:

Object.defineProperty(Array.prototype, "maprec", {
  value: function(cb){
      return this.map(function(v){ return cb.call(v) })
  }
});
console.log([' aa ', ' bb '].maprec(String.prototype.trim)); // logs ["aa", "bb"]

I said "mostly for fun" because modifying objects you don't own (Array's prototype here) is widely seen as a bad practice. But you could also make a utilitarian function taking both the array and the callback as arguments.

我说“主要是为了好玩”,因为修改你不拥有的对象(这里是 Array 的原型)被广泛认为是一种不好的做法。但是您也可以创建一个将数组和回调都作为参数的实用函数。

回答by yckart

I just compared some ways to trim an array of strings to get the shortest and fastest method. Who is interested in, here is a performance test on jsperf: http://jsperf.com/trim-array-of-strings

我只是比较了一些修剪字符串数组的方法,以获得最短和最快的方法。谁有兴趣,这里是jsperf的性能测试:http://jsperf.com/trim-array-of-strings

var chunks = "  .root  ,  .parent  >  .child  ".split(',')
var trimmed1 = chunks.map(Function.prototype.call, String.prototype.trim);
var trimmed2 = chunks.map(function (str) { return str.trim(); });
var trimmed3 = chunks.map(str => str.trim());
var trimmed4 = $.map(chunks, $.trim);

Note: jQuery is just here to compare the number of characters to type ;)

注意:jQuery 只是在这里比较要输入的字符数;)

回答by Martín Silva

Keep it simple and stupid:

保持简单和愚蠢:

Code

代码

[' aa ', ' b b ', '   c c '].map(i=>i.trim());

Output

输出

["aa", "b b", "c c"]

回答by Redu

Influencing from Bergi's perfect answer, i just would like to add, for those methods which won't take a thisargument, you may achieve the same job as follows;

受Bergi完美答案的影响,我只想补充一点,对于那些不带this参数的方法,您可以实现以下相同的工作;

var x = [' aa ', ' bb '],
    y = x.map(Function.prototype.call.bind(String.prototype.trim))

回答by Amr

var x = [" aa ", " bb "];
console.log(x); // => [" aa ", " bb "]

// remove whitespaces from both sides of each value in the array
x.forEach(function(value, index){
  x[index] = value.trim();
});

console.log(x); // => ["aa", "bb"]

All major browsers support forEach(), but note that IEsupports it only beginning from version 9.

所有主流浏览器都支持forEach(),但请注意,IE仅从版本 9 开始支持。

回答by gildniy

Another ES6 alternative

另一个 ES6 替代品

const row_arr = ['a ', ' b' , ' c ', 'd'];
const trimed_arr = row_arr.map(str => str.trim());
console.log(trimed_arr); // <== ['a', 'b', 'c', 'd']