var countMatches = function(items, ruleKey, ruleValue) {
const map = {
type: 0,
color: 1,
name: 2
}
return items.reduce((acc, cur) => cur[map[ruleKey]] === ruleValue ? acc + 1 : acc, 0)
};
먼저
ruleKey와 인덱스를 매핑한 객체map를 만들었다. 그래야 한 아이템에서ruleValue를 뽑아내기 쉬워진다.
그 다음 reduce 메소드를 이용해서ruleValue가 일치할 경우 수를 하나씩 증가시키면된다.