2016-09-09 2 views
1

2 개의 문자열 배열이 있습니다. 두 번째 배열의 모든 요소가 첫 번째 배열에 있는지 확인하려고합니다. 나는 이런 일에 Lodash/Underscore를 사용합니다. 하나의 배열이 배열에 있는지 검사 할 때 편리합니다 :배열의 모든 요소가 다른 배열에 있는지 확인하는 Lodash 메서드

그러나 배열 일 때, 나는 그것을 할 수있는 현재 방법을 볼 수 없습니다. 내가 한 일은 다음과 같습니다.

var arr1 = ['a', 'b', 'c', 'd']; 
var arr2 = ['a', 'b', 'x']; 

var intersection = _.intersection(arr1, arr2); 

console.log('intersection is ', intersection); 

if (intersection.length < arr2.length) { 
    console.log('no'); 
} else { 
    console.log('yes'); 
} 

피들은 here입니다. 그러나 그것의 오랜 바람 -. 내장 된 Lodash 방법이 있습니까?

답변

1

대칭 차이는 _.xor을 사용하고 길이를 수표로 사용할 수 있습니다. length === 0의 경우 두 배열에는 모두 같은 요소가 들어 있습니다.

var arr1 = ['a', 'b', 'c', 'd'], 
 
    arr2 = ['a', 'b', 'x']; 
 

 
console.log(_.xor(arr2, arr1));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

관련 문제