![](https://habrastorage.org/getpro/habr/post_images/05f/584/061/05f584061a5d4c4e44f1c9aea977a0db.png)
1.アレイのクリーニングまたは切り捨て
配列をクリアするか、その長さを減らすために、その
length
プロパティを変更できます。 同時に、たとえば、新しい配列へのリンクを、変数へのリンクを格納する変数に書き込んで、配列をクリーンにする必要はありません。
const arr = [11, 22, 33, 44, 55, 66]; // arr.length = 3; console.log(arr); //=> [11, 22, 33] // arr.length = 0; console.log(arr); //=> [] console.log(arr[2]); //=> undefined
2.オブジェクトの破壊中の名前付きパラメーターのシミュレーション
パラメータの変数セットを何らかの関数に渡す必要がある場合、パラメータを持つオブジェクトをすでに使用している可能性が非常に高くなります。 たとえば、次のようになります。
doSomething({ foo: 'Hello', bar: 'Hey!', baz: 42 }); function doSomething(config) { const foo = config.foo !== undefined ? config.foo : 'Hi'; const bar = config.bar !== undefined ? config.bar : 'Yo!'; const baz = config.baz !== undefined ? config.baz : 13; // ... }
これは、JavaScriptの名前付きパラメーターをシミュレートするために使用される、古くて効果的なパターンです。 このような関数の呼び出しは非常に正常に見えますが、一方で、パラメーターを使用してオブジェクトを処理するロジックを実装するには、多くのコードが必要です。 ES2015オブジェクトを再構築する機能により、この欠点を回避できます。
function doSomething({ foo = 'Hi', bar = 'Yo!', baz = 13 }) { // ... }
また、パラメーターをオプションにしたオブジェクトを作成する必要がある場合、これも問題を引き起こしません。
function doSomething({ foo = 'Hi', bar = 'Yo!', baz = 13 } = {}) { // ... }
3.再構築と配列要素
以下は、破壊機能を使用して配列要素を個々の変数に割り当てる方法です。
const csvFileLine = '1997,John Doe,US,john@doe.com,New York'; const { 2: country, 4: state } = csvFileLine.split(',');
4. switchステートメントで値の範囲を使用する
switch
での値の範囲の使用を示す簡単なトリックを次に示します。
function getWaterState(tempInCelsius) { let state; switch (true) { case (tempInCelsius <= 0): state = 'Solid'; break; case (tempInCelsius > 0 && tempInCelsius < 100): state = 'Liquid'; break; default: state = 'Gas'; } return state; }
5. async / awaitコンストラクト内のいくつかの非同期関数の実行を待機する組織
Promise.all
を使用する次のトリックのおかげで、いくつかの非同期関数が完了するのを待つことができます。
await Promise.all([anAsyncCall(), thisIsAlsoAsync(), oneMore()])
6.クリーンオブジェクトの作成
必要に応じて、
Object
からプロパティとメソッドを継承しない、完全に空のクリーンなオブジェクトを作成できます(たとえば、これは
constructor
、
toString()
)。 方法は次のとおりです。
const pureObject = Object.create(null); console.log(pureObject); //=> {} console.log(pureObject.constructor); //=> undefined console.log(pureObject.toString); //=> undefined console.log(pureObject.hasOwnProperty); //=> undefined
7. JSONコードのフォーマット
JSON.stringify
メソッドは、オブジェクトを文字列表現に変換する
JSON.stringify
はあり
JSON.stringify
。 特に、このメソッドを使用して取得したJSONコードはフォーマットできます。
const obj = { foo: { bar: [11, 22, 33, 44], baz: { bing: true, boom: 'Hello' } } }; // - , // JSON-. JSON.stringify(obj, null, 4); // =>"{ // => "foo": { // => "bar": [ // => 11, // => 22, // => 33, // => 44 // => ], // => "baz": { // => "bing": true, // => "boom": "Hello" // => } // => } // =>}"
8.重複する配列要素を削除する
ES2015の
Set
タイプのオブジェクトを拡張演算子とともに使用すると、配列から重複する要素を簡単に削除できます。
const removeDuplicateItems = arr => [...new Set(arr)]; removeDuplicateItems([42, 'foo', 42, 'foo', true, true]); //=> [42, "foo", true]
9.多次元配列の線形化
拡張演算子を使用した配列の線形化は非常に簡単なタスクです。
const arr = [11, [22, 33], [44, 55], 66]; const flatArr = [].concat(...arr); //=> [11, 22, 33, 44, 55, 66]
残念ながら、上記の手法は2次元配列に対してのみ機能します。 ただし、再帰のおかげで、2次元以上の配列に対してこのメソッドを使用できます。
function flattenArray(arr) { const flattened = [].concat(...arr); return flattened.some(item => Array.isArray(item)) ? flattenArray(flattened) : flattened; } const arr = [11, [22, 33], [44, [55, 66, [77, [88]], 99]]]; const flatArr = flattenArray(arr); //=> [11, 22, 33, 44, 55, 66, 77, 88, 99]
まとめ
この資料から学んだテクニックが、JavaScriptプログラムの改善と時間の節約に役立つことを願っています。
親愛なる読者! JS開発者にとって有用な非自明なことを知っていますか? もしそうなら、それらについて教えてください。
![](https://habrastorage.org/files/1ba/550/d25/1ba550d25e8846ce8805de564da6aa63.png)