[JavaScript] 배열 합집합, 교집합 반환하기
합집합const arrA = [1, 4, 3, 2]; const arrB = [5, 2, 6, 7, 1];const result = [...new Set([...arrA, ...arrB])]; // [1, 4, 3, 2, 5, 6, 7] 교집합const arrA = [1, 4, 3, 2]; const arrB = [5, 2, 6, 7, 1];const result = arrA.filter(it => arrB.includes(it)); // [1, 2]
2023. 12. 30.
[JavaScript] 숫자/문자열 배열 중복 제거 set, map, reduce, filter
Setconst values = [3, 1, 3, 5, 2, 4, 4, 4];const uniqueValues = [...new Set(values)]; // [3, 1, 5, 2, 4] mapconst list = [3, 1, 3, 5, 2, 4, 4, 4]; const result = [...new Map(list.map(item => [item, item])).values()];console.log(result); // [3, 1, 5, 2, 4] reduceconst list = [3, 1, 3, 5, 2, 4, 4, 4];const result = list.reduce((accumulator, currentValue) => { if (!accumulator.includes(currentValu..
2023. 12. 30.