4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScript / Setオブジェクトで文字列を1文字単位でまとめて格納する

4
Posted at

JavaScriptのSetオブジェクトに、文字を1文字ずつ簡単に格納する方法について書いておきます。
後者の書き方で記載すれば、1文字ずつカンマで区切る必要がなくなります。

1. 標準的な書き方

sample.js
const kans = new Set(['', '', '', '', '', '', '', '', '', '']);

console.log(kans.has(''));
// 表示結果 true

console.log(kans.has(''));
// 表示結果 true

console.log(kans.has(''));
// 表示結果 false

2. 簡単な書き方

sample.js
const kans2 = new Set('〇一二三四五六七八九');

console.log(kans2.has(''));
// 表示結果 true

console.log(kans2.has('二三'));
// 表示結果 false

console.log(kans2.has(''));
// 表示結果 false

pythonでできる方法なので、試してみたら上手くいきました。
記事が見当たらなかったので(探せばあると思いますが)、備忘として残しておきます。

●参考とした記事
MDN Web Docs - Set

4
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?