Set Methods ใหม่ ES2025 — union, intersection, difference
ES2025 เพิ่ม set operation methods เข้ามาใน Set โดยตรง ไม่ต้องเขียนเอง
const frontend = new Set(['html', 'css', 'js', 'ts']);
const backend = new Set(['js', 'ts', 'python', 'sql']);
// Union — รวมทุกอย่างจากทั้งสอง set
frontend.union(backend);
// Set { 'html', 'css', 'js', 'ts', 'python', 'sql' }
// Intersection — เฉพาะที่อยู่ทั้งคู่
frontend.intersection(backend);
// Set { 'js', 'ts' }
// Difference — อยู่ใน A แต่ไม่อยู่ใน B
frontend.difference(backend);
// Set { 'html', 'css' }
// Symmetric difference — อยู่ใน A หรือ B แต่ไม่ทั้งคู่
frontend.symmetricDifference(backend);
// Set { 'html', 'css', 'python', 'sql' }
// isSubsetOf / isSupersetOf / isDisjointFrom
new Set(['js', 'ts']).isSubsetOf(frontend); // true
Browser support: Chrome 122+, Firefox 127+, Safari 17+
ก่อนหน้านี้ต้องเขียน:
const intersection = new Set([...a].filter(x => b.has(x)));