let output = test([
{
"id": 1,
"title": "자동차 시제품 제작",
"client": "A 고객사",
"due": "2020.12.14",
"count": 2,
"amount": 100,
"method": [
"밀링",
"선반"
],
"material": [
"알루미늄"
],
"status": "대기중"
},
{
"id": 2,
"title": "비행기 시제품 제작",
"client": "B 고객사",
"due": "2020.12.13",
"count": 2,
"amount": 100,
"method": [
"선반"
],
"material": [
"탄소강",
"강철"
],
"status": "상담중"
},
{
"id": 3,
"title": "기차 시제품 제작",
"client": "C 고객사",
"due": "2020.12.12",
"count": 1,
"amount": 20,
"method": [
"선반"
],
"material": [
"구리"
],
"status": "대기중"
},
{
"id": 4,
"title": "자전거 시제품 제작",
"client": "D 고객사",
"due": "2020.12.11",
"count": 1,
"amount": 45,
"method": [
"선반"
],
"material": [
"스테인리스강"
],
"status": "대기중"
},
{
"id": 5,
"title": "헬리콥터 시제품 제작",
"client": "E 업체",
"due": "2020.12.10",
"count": 2,
"amount": 2,
"method": [
"밀링"
],
"material": [
"알루미늄",
"탄소강"
],
"status": "대기중"
},
{
"id": 6,
"title": "전동 킥보드 시제품 제작",
"client": "F 업체",
"due": "2020.12.09",
"docs": 1,
"amount": 20,
"method": [
"밀링"
],
"material": [
"강철"
],
"status": "대기중"
}
], ["밀링","선반"], ["알루미늄"])
console.log(output)
function test(arr, arr2, arr3) {
let methodfilter = []
arr.forEach((data) => {
let find = data.method.find((el) => arr2.includes(el))
if(data.method.includes(find)){
methodfilter.push(data)
}
})
let result = []
methodfilter.forEach((data) => {
let find2 = data.material.find((el) => arr3.includes(el))
if(data.material.includes(find2)){
result.push(data)
}
})
return result
}
두 배열의 교집합을 찾을 때 filter
와 includes
를 사용하여 두 배열에 포함된 동일한 요소를 확인 할 수 있는 방법도 있었지만 나는 find
를 사용하여 includes
조건을 만족하는 첫 번째 요소의 값을 반환
하는 방법을 이용하였다. 그 결과값을 포함하고 있는 값들만을 조건을 주어 새로운 배열에 담아주었다.
[
{
id: 1,
title: '자동차 시제품 제작',
client: 'A 고객사',
due: '2020.12.14',
count: 2,
amount: 100,
method: [ '밀링', '선반' ],
material: [ '알루미늄' ],
status: '대기중'
},
{
id: 5,
title: '헬리콥터 시제품 제작',
client: 'E 업체',
due: '2020.12.10',
count: 2,
amount: 2,
method: [ '밀링' ],
material: [ '알루미늄', '탄소강' ],
status: '대기중'
}
]