지금까지 코테를 치면서 2차원 배열을 활용해서 key,value를 두는 정말 바보 같은 짓을 많이 했던 것 같았다. 예를들어 랭킹을 세는 것, 배열의 몇 번째의 인덱스를 추출하거나, 두 배열을 활용해서 값을 나타날때 등 그렇게 활용했는데 객체를 활용해서 배열로 다시 추출하는 방법이 있었다.
Object.keys(obj)- 객체의 키만 담은 배열 반환
Object.values(obj) - 객체의 값만 담은 배열 반환
Object.entries(obj) - [키,값] 쌍을 담은 배열 반환
활용한 예시
2021 KAKAO BLINE RECRUITMENT
메뉴 리뉴얼
function solution(orders, course) {
let answer = [];
const list = {};
const getCombination = (arr, n) => {
const result = [];
if(n === 1) return arr.map(e => [e]);
arr.forEach((e, idx, origin) => {
const rest = origin.slice(idx + 1);
const combinations = getCombination(rest, n-1);
const attached = combinations.map(combi => [e, ...combi]);
result.push(...attached);
});
return result;
}
orders.map((order) => {
const orderArr = order.split('').sort();
//만들수 있는 조합 메뉴의 수는 2부터 시작해서 최대 주문된 구성 수까지
for(let i = 2; i <= orderArr.length; i++) {
//만약 조합메뉴의 수가 스카피가 원하는 조합메뉴 수와 일치하지 않는다면 넘어감
//구해봤자 스카피는 원하지 않기 때문
if(!course.includes(i)) continue;
const orderCombis = getCombination(orderArr, i);
//list라는 Object에 key = 조합 value = 같은 조합이 주문된 수를 넣어줌
orderCombis.map(orderCombi => {
const string = orderCombi.join('');
list[string] = list[string]? list[string] + 1 : 1;
});
};
});
let listArr = Object.entries(list);
console.log(listArr)
course.map(c => {
//스카피가 원하는 조합메뉴 수와 일치하는 조합과 최소 2번 이상 주문된 조합을 필터링해준다.
const candidates = listArr.filter(e => e[0].length === c && e[1] > 1);
if(candidates.length > 0) {
//해당하는 조합에서 가장 많이 주문된 주문 수 구하기
let max = Math.max(...candidates.map(e => e[1]));
//가장 많이 주문된 조합을 answer 배열에 push 해줌
candidates.map(e => {
if(e[1] === max) answer.push(e[0]);
});
}
})
//마지막으로 알파벳 오름차순으로 정렬해서 return 하기
return answer.sort();
};
여기서
let listArr =Object.entries(list);
객체의 키값은 조합의 경우의 수 ex) "AB","AC","ACE"
객체의 value값은 조합의 횟수 ex) 1,2,3,4
객체의 한 쌍을 배열로 받아서 저장 ex) ["AB",1],["AC",2]