Array method 실무형 퀴즈 4 (주관식)
1. 객체 배열에서 특정 속성 값만 추출
Ghost는 친구 객체 배열에서 이름만 뽑아 새로운 배열을 만들고 싶습니다. friends 배열에서 name 속성만 추출해 반환하는 solution
함수를 완성하세요. 예시: [{name: 'Irangi', age: 21}, ...]
function solution(friends) {
// 여기에 코드를 작성하세요
}
console.log(solution([
{name: 'Irangi', age: 21},
{name: 'Camel', age: 24},
{name: 'Mui', age: 19}
])); // ['Irangi', 'Camel', 'Mui']
힌트보기
map(콜백): 객체의 특정 속성만 추출할 수 있습니다. 예) arr.map(x => x.name)
정답보기
코드실행
function solution(friends) {
return friends.map(x => x.name);
}
console.log(solution([
{name: 'Irangi', age: 21},
{name: 'Camel', age: 24},
{name: 'Mui', age: 19}
]));
2. 객체 배열에서 조건에 맞는 객체 찾기
Ghost는 나이가 20살 이상인 친구 중 첫 번째를 찾고 싶습니다. friends 배열에서 age가 20 이상인 첫 번째 객체를 반환하는
solution 함수를 완성하세요.
function solution(friends) {
// 여기에 코드를 작성하세요
}
console.log(solution([
{name: 'Irangi', age: 19},
{name: 'Camel', age: 22},
{name: 'Mui', age: 25}
])); // {name: 'Camel', age: 22}
힌트보기
find(콜백): 조건에 맞는 첫 번째 객체를 반환합니다. 예) arr.find(x => x.age >= 20)
정답보기
코드실행
function solution(friends) {
return friends.find(x => x.age >= 20);
}
console.log(solution([
{name: 'Irangi', age: 19},
{name: 'Camel', age: 22},
{name: 'Mui', age: 25}
]));
3. 중복 제거(유니크 값 추출)
Ghost는 친구들이 좋아하는 색상 배열에서 중복을 제거하고 싶습니다. colors 배열에서 중복을 제거한 새 배열을 반환하는 solution 함수를
완성하세요.
function solution(colors) {
// 여기에 코드를 작성하세요
}
console.log(solution(['red', 'blue', 'red', 'green', 'blue'])); // ['red', 'blue', 'green']
힌트보기
Set을 활용하거나 filter+indexOf를 사용할 수 있습니다. 예) [...new Set(arr)] 또는 arr.filter((v,i,a) => a.indexOf(v)
=== i)
정답보기
코드실행
function solution(colors) {
return [...new Set(colors)];
}
console.log(solution(['red', 'blue', 'red', 'green', 'blue']));
4. 객체 배열에서 평균 구하기
Ghost는 친구들의 점수 평균을 구하고 싶습니다. scores 배열에서 모든 score의 평균을 반환하는 solution 함수를
완성하세요. 예시: [{name: 'Irangi', score: 80}, ...]
function solution(scores) {
// 여기에 코드를 작성하세요
}
console.log(solution([
{name: 'Irangi', score: 80},
{name: 'Camel', score: 90},
{name: 'Mui', score: 100}
])); // 90
힌트보기
map+reduce 조합 또는 reduce만으로 합산 후 개수로 나누세요. 예) arr.reduce((acc, cur) => acc + cur.score, 0) /
arr.length
정답보기
코드실행
function solution(scores) {
return scores.reduce((acc, cur) => acc + cur.score, 0) / scores.length;
}
console.log(solution([
{name: 'Irangi', score: 80},
{name: 'Camel', score: 90},
{name: 'Mui', score: 100}
]));
5. 다단계 변환(객체 배열 → 이름+점수 문자열 배열)
Ghost는 친구들의 이름과 점수를 "이름:점수" 형태의 문자열로 변환하고 싶습니다. scores 배열을 변환해 반환하는 solution 함수를
완성하세요. 예시: [{name: 'Irangi', score: 80}, ...] → ['Irangi:80', ...]
function solution(scores) {
// 여기에 코드를 작성하세요
}
console.log(solution([
{name: 'Irangi', score: 80},
{name: 'Camel', score: 90}
])); // ['Irangi:80', 'Camel:90']
힌트보기
map(콜백): 여러 속성을 조합해 문자열로 만들 수 있습니다. 예) arr.map(x => `${x.name}:${x.score}`)
정답보기
코드실행
function solution(scores) {
return scores.map(x => `${x.name}:${x.score}`);
}
console.log(solution([
{name: 'Irangi', score: 80},
{name: 'Camel', score: 90}
]));
6. 그룹화(카테고리별로 묶기)
Ghost는 친구들의 취미별로 그룹을 만들고 싶습니다. friends 배열을 hobby 속성별로 그룹화하여 객체로 반환하는 solution 함수를
완성하세요. 예시: [{name: 'Irangi', hobby: '축구'}, ...] → {축구: [...], 농구: [...]}
function solution(friends) {
// 여기에 코드를 작성하세요
}
console.log(solution([
{name: 'Irangi', hobby: '축구'},
{name: 'Camel', hobby: '농구'},
{name: 'Mui', hobby: '축구'}
])); // {축구: [{...}, {...}], 농구: [{...}]}
힌트보기
reduce를 활용해 객체에 배열을 누적하세요. 예) arr.reduce((acc, cur) => { ... }, {})
정답보기
코드실행
function solution(friends) {
return friends.reduce((acc, cur) => {
acc[cur.hobby] = acc[cur.hobby] || [];
acc[cur.hobby].push(cur);
return acc;
}, {});
}
console.log(solution([
{name: 'Irangi', hobby: '축구'},
{name: 'Camel', hobby: '농구'},
{name: 'Mui', hobby: '축구'}
]));
7. 다중 조건 필터링
Ghost는 20살 이상이면서 취미가 '축구'인 친구만 추출하고 싶습니다. friends 배열에서 조건에 맞는 객체만 추출해 반환하는 solution
함수를 완성하세요.
function solution(friends) {
// 여기에 코드를 작성하세요
}
console.log(solution([
{name: 'Irangi', age: 21, hobby: '축구'},
{name: 'Camel', age: 19, hobby: '농구'},
{name: 'Mui', age: 22, hobby: '축구'}
])); // [{name: 'Irangi', ...}, {name: 'Mui', ...}]
힌트보기
filter(콜백): 여러 조건을 &&로 결합하세요. 예) arr.filter(x => x.age >= 20 && x.hobby === '축구')
정답보기
코드실행
function solution(friends) {
return friends.filter(x => x.age >= 20 && x.hobby === '축구');
}
console.log(solution([
{name: 'Irangi', age: 21, hobby: '축구'},
{name: 'Camel', age: 19, hobby: '농구'},
{name: 'Mui', age: 22, hobby: '축구'}
]));
8. 다중 정렬(여러 기준으로 정렬)
Ghost는 친구들을 나이 내림차순, 이름 오름차순(동일 나이일 때)으로 정렬하고 싶습니다. friends 배열을 정렬해 반환하는 solution 함수를
완성하세요.
function solution(friends) {
// 여기에 코드를 작성하세요
}
console.log(solution([
{name: 'Irangi', age: 21},
{name: 'Camel', age: 22},
{name: 'Mui', age: 21}
])); // [{name: 'Camel', age: 22}, {name: 'Irangi', age: 21}, {name: 'Mui', age: 21}]
힌트보기
sort(비교함수): 여러 기준은 if/else 또는 || 연산으로 결합합니다. 예) arr.sort((a, b) => b.age - a.age ||
a.name.localeCompare(b.name))
정답보기
코드실행
function solution(friends) {
return friends.sort((a, b) => b.age - a.age || a.name.localeCompare(b.name));
}
console.log(solution([
{name: 'Irangi', age: 21},
{name: 'Camel', age: 22},
{name: 'Mui', age: 21}
]));
9. 플랫(flat)과 중첩 배열 펼치기
Ghost는 친구별로 여러 개의 취미를 가진 배열을 하나의 평면 배열로 만들고 싶습니다. hobbies 배열을 평탄화(flat)하여 반환하는
solution 함수를 완성하세요. 예시: [['축구', '농구'], ['야구'], ['수영', '독서']] → ['축구', '농구', '야구', '수영', '독서']
function solution(hobbies) {
// 여기에 코드를 작성하세요
}
console.log(solution([
['축구', '농구'],
['야구'],
['수영', '독서']
])); // ['축구', '농구', '야구', '수영', '독서']
힌트보기
flat() 또는 reduce+concat을 사용할 수 있습니다. 예) arr.flat() 또는 arr.reduce((acc, cur) => acc.concat(cur),
[])
정답보기
코드실행
function solution(hobbies) {
return hobbies.flat();
}
console.log(solution([
['축구', '농구'],
['야구'],
['수영', '독서']
]));
10. 객체 배열에서 최대/최소 찾기
Ghost는 친구들 중 점수가 가장 높은 사람과 가장 낮은 사람을 각각 찾고 싶습니다. scores 배열에서 최고점/최저점 객체를 반환하는
solution 함수를 완성하세요. 예시: [{name: 'Irangi', score: 80}, ...]
function solution(scores) {
// 여기에 코드를 작성하세요
}
console.log(solution([
{name: 'Irangi', score: 80},
{name: 'Camel', score: 95},
{name: 'Mui', score: 70}
])); // {max: {name: 'Camel', score: 95}, min: {name: 'Mui', score: 70}}
힌트보기
reduce를 활용해 최대/최소 객체를 누적하세요. 예) arr.reduce((acc, cur) => {...}, {max:..., min:...})
정답보기
코드실행
function solution(scores) {
return scores.reduce((acc, cur) => {
if (!acc.max || cur.score > acc.max.score) acc.max = cur;
if (!acc.min || cur.score < acc.min.score) acc.min = cur;
return acc;
}, {max: null, min: null});
}
console.log(solution([
{name: 'Irangi', score: 80},
{name: 'Camel', score: 95},
{name: 'Mui', score: 70}
]));