TypeScript  concat()で配列(array)同士を重複なしで結合するサンプル

環境
Windows 11 Pro 64bit
TypeScript 4.4.4

構文
const unique = new Set(配列1.concat(配列2)); #配列1、配列2を結合する
const result = […unique]; #配列要素を重複なしでSetを取得する
concat()を使って2つの配列(array)同士を重複なしで結合するには、Setを使います。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arrayA = [21, 32, 43, 84, 95];
let arrayB = [43, 44, 25, 36, 67];
const res= new Set(arrayA.concat(arrayB));
console.log(res);
let arrayA = [21, 32, 43, 84, 95]; let arrayB = [43, 44, 25, 36, 67]; const res= new Set(arrayA.concat(arrayB)); console.log(res);
let arrayA = [21, 32, 43, 84, 95];
let arrayB = [43, 44, 25, 36, 67];

const res= new Set(arrayA.concat(arrayB)); 

console.log(res);

実行結果
[LOG]: Set (9) {21, 32, 43, 84, 95, 44, 25, 36, 67}

TypeScript

Posted by arkgame