Set.prototype.symmetricDifference()
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Set
인스턴스의 symmetricDifference()
메서드는 하나의 Set을 받아서 이 Set과 주어진 Set 중 하나에는 있지만 둘 다에 없는 요소를 포함하는 새 Set을 반환합니다.
구문
symmetricDifference(other)
매개변수
반환 값
이 Set과 주어진 Set 중 하나에는 있지만 둘 다에 없는 요소를 포함하는 Set
객체.
설명
수학적 표기법으로는 대칭차(symmetric difference)는 아래와 같이 표현할 수 있습니다.
벤 다이어그램을 사용하면 다음과 같습니다.
symmetricDifference()
는 other
매개변수로 유사 Set 객체를 허용합니다. 어떠한 사용자 코드 호출없이 this
에 저장된 데이터를 직접 검색하기 때문에 this
는 실제 Set
인스턴스여야 합니다. 그 다음 keys()
메서드를 호출하여 other
를 순회하고 other
에서 보이지 않는 this
의 모든 요소와 this
에서 보이지 않는 other
의 모든 요소로 새 집합을 구성합니다.
반환된 집합의 요소 순서는 먼저 this
에 있는 요소, 그 다음에 other
에 있는 요소입니다.
예제
symmetricDifference() 사용하기
다음 예는 짝수 집합(<10)과 완전 제곱 집합(<10) 사이의 대칭차집합를 계산하는 예입니다. 결과는 짝수 또는 완전 제곱 중 하나이지만 둘 다 아닌 숫자 집합입니다.
const evens = new Set([2, 4, 6, 8]);
const squares = new Set([1, 4, 9]);
console.log(evens.symmetricDifference(squares)); // Set(5) { 2, 6, 8, 1, 9 }
명세서
Specification |
---|
Set methods # sec-set.prototype.symmetricDifference |
브라우저 호환성
BCD tables only load in the browser