JavaScript mapメソッドを使って配列内の値の前後の空白を除去する方法

環境
Google Chrome 109.0.5414.120
Windows 10 Home 64bit

書式
1.配列の宣言
const 配列名 =[要素1,要素2,…]

2.配列名.map( function(引数){
引数.trim()
処理コード
}
trimメソッドを使って配列内の要素の前後の空白を除去します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const arr = [ ' s ', null, NaN, 88, ' s m ' ];
const res = arr.map( function(ee) {
if (typeof ee === 'string') return ee.trim();
return ee;
});
console.log(res);
const arr = [ ' s ', null, NaN, 88, ' s m ' ]; const res = arr.map( function(ee) { if (typeof ee === 'string') return ee.trim(); return ee; }); console.log(res);
const arr = [ ' s ', null, NaN, 88, ' s m ' ];

const res = arr.map( function(ee) {

  if (typeof ee === 'string') return ee.trim();

  return ee;

});

console.log(res);

実行結果
> Array [“s", null, NaN, 88, “s m"]

JavaScript

Posted by arkgame