blog

ES6 Essentials 07-配列拡張メソッド

ES6 Essentials - 配列展開メソッド 配列メソッド: 1. from: 疑似配列を実配列に変換 2. of: 任意のデータ型を配列に変換 3....

Jul 22, 2020 · 2 min. read
シェア

配列のメソッド:

1.from: 疑似配列から実配列への変換

// 1.from() 疑似配列を実配列に変換する function add() { // console.log(arguments); // es5 // let arr = [].slice.call(arguments); // console.log(arr); // es6 let arr = Array.from(arguments); console.log(arr); } add(1, 2, 3); //実際の使用例 let lis = document.querySelectorAll('li') console.log(lis);//Arguments console.log(Array.from(lis)); //配列を返す // 擬似配列を実配列に変換する拡張演算子 console.log([...lis]); //配列を返す // from() 第2引数を取ることもでき、各要素に対して使用できる。 let liContents = Array.from(lis, ele => ele.textContent); console.log(liContents); //与えられた li 要素の中にないテキスト値を出力する

2.of: 任意のデータ型を受け取り、それを配列に変換します。

console.log(Array.of(3, 11, 20, [1, 2, 3], { id: 1 })); //[3,11,20,[1,2,3]]

3.find(): 条件にマッチする最初の配列メンバーを見つける

// find()条件にマッチする最初の配列メンバを見つける let num = [1, 2, -10, -20, 9, 2].find(n => n < 0) // console.log(num);

.findIndex():対象となる最初の配列メンバのインデックスを見つけます。

let numIndex = [1, 2, -10, -20, 9, 2].findIndex(n => n < 0) console.log(numIndex); //2

.entries() keys() values() イテレータを返します。ループの

  • keys() キー名の反復処理
  • values() 値に対する繰り返し処理
  • entries() キーと値のペアの繰り返し処理
//イテレータを返す console.log(['a','b'].keys()); //Array Iterator {} console.log(['a','b'].values()); //Array Iterator {} console.log(['a','b'].entries()); //Array Iterator {} for (let index of ['a', 'b'].keys()) { console.log(index); //0,1 } for (let ele of ['a', 'b'].values()) { console.log(ele); // a,b } for(let [index,ele] of ['a','b'].entries()){ console.log(index,ele); // 0 ” 1 ” } //反復子 nex メソッド let letter = ['a','b','c']; //配列の entries はイテレータを返す。 let it = letter.entries(); console.log(it.next().value); //最初の走査の値を返す console.log(it.next().value); //2回目の探索の値を返す1 b console.log(it.next().value); //3回目の走査の値を返す2 c console.log(it.next().value); //未定義を返す。

6.includes()は、指定された配列が指定された値を含むかどうかを示すブール値を返します。

//es6 console.log([1,2,3].includes(2)); //true console.log([1,2,3].includes(4)); //false //es5 console.log([1,2,3].indexOf(2)); //true
Read next

OpenGL-トンネルのケーススタディ

このケーススタディは、主にこのような効果を達成するためにアニメーションを変更するには、4つのテクスチャを描画する必要があり、左右の壁のテクスチャ、トンネルの上部とテクスチャの下部は、同じ全体の順序を準備する関数です→SetupRC関数→RenderSce

Jul 22, 2020 · 6 min read