LoginSignup
7
2

More than 3 years have passed since last update.

jsで文字列の重複を削除する

Posted at

jsで文字列の重複を削除したい。
配列の重複はやり方が見つかったのですが、文字列のが見つからなかったので、こんな感じでやってみました。

メソッドチェーン一回したのですが、なぜなエラーになるのと、チェーンつなぎすぎて読みづらいのでやめました。

let tmp = '123123123';

tmp = tmp.split('');
tmp = tmp.filter(function (x, i, self) {
  return self.indexOf(x) === i;
});

tmp = tmp.join(',');
tmp = tmp.replace(/,/g, '');

console.log(tmp);
// 123

参考ページ

配列の重複をはじく、もしくは重複を取り出す
https://qiita.com/cocottejs/items/7afe6d5f27ee7c36c61f

7
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
2