Javascript - Apply trim function to each string in an array

后端 未结 11 1339
-上瘾入骨i
-上瘾入骨i 2020-12-07 14:22

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

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

output

[\'aa\', \'bb\']

My fi

11条回答
  •  轮回少年
    2020-12-07 14:47

    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 IE supports it only beginning from version 9.

提交回复
热议问题