0
0

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 1 year has passed since last update.

[TypeScript]Array関連のメソッドの挙動 concat/filter/flat/includes

Posted at

concat

  • 2つ以上の配列を結合するために使用
  • 既存の配列を変更せず、新しい配列を返す
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);

console.log(array3);
// => Array ["a", "b", "c", "d", "e", "f"]

filter

  • 指定された配列の中から指定された関数で実装されているテストに合格した要素だけを抽出
const words = ['spray', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter((word) => word.length > 6);

console.log(result);
// => Array ["exuberant", "destruction", "present"]

flat

  • すべてのサブ配列の要素を指定した深さで再帰的に結合した新しい配列を生成
const arr1 = [0, 1, 2, [3, 4]];

console.log(arr1.flat());
// => Array [0, 1, 2, 3, 4]

const arr2 = [0, 1, [2, [3, [4, 5]]]];

console.log(arr2.flat());
// => Array [0, 1, 2, Array [3, Array [4, 5]]]

console.log(arr2.flat(2));
// => Array [0, 1, 2, 3, Array [4, 5]]

console.log(arr2.flat(Infinity));
// => Array [0, 1, 2, 3, 4, 5]

includes

  • 特定の要素が配列に含まれているかどうかを true または false で返す
const array1 = [1, 2, 3];

console.log(array1.includes(2));
// => true

const pets = ['cat', 'dog', 'bat'];

console.log(pets.includes('cat'));
// => true

console.log(pets.includes('at'));
// => false
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?