.jpeg)
1773. Count Items Matching a Rule
문제
배열 items와 문자열 데이터 ruleKey, ruleValue가 매개변수로 주어졌을 때 ,(items[i] 는 i번째의 [type,color,name]으로 이루어져있다) 조건을 만족하는 원소의 개수를 return 하는 문제
가정
1. items는 길이가 1이상 10,000이하인 배열
2. items[i]의 [type,color,name]과 ruleValue의 길이는 1이상 10이하
3. ruleKey는 type,color,name
4. 모든 문자열은 소문자로 이루어짐
풀이var countMatches = function(items, ruleKey, ruleValue) { let itemsType = 0; switch(ruleKey){ case "type" : itemsType = 0; break; case "color" : itemsType = 1; break; case "name" : itemsType = 2; break; default : itemsType = 0; } const result = items.filter((data)=>data[itemsType]===ruleValue).length return result; };