JavaScript 데이터01

문제 1: 배열 필터링 및 변환 (초급)

목표:

  • activetrue인 사용자만 필터링하세요.
  • 필터링된 사용자들의 name만 추출해 새로운 배열로 만드세요.
const users = [
  { id: 1, name: "Alice", age: 25, active: true },
  { id: 2, name: "Bob", age: 30, active: false },
  { id: 3, name: "Charlie", age: 22, active: true },
  { id: 4, name: "David", age: 35, active: false },
];

const activeUserNames = users
  .filter((user) => user.active)
  .map((user) => user.name);

console.log(activeUserNames); // ["Alice", "Charlie"]

문제 2: 데이터 합계 계산 (초급)

목표:

  • 모든 amount의 합계를 구하세요.
const orders = [
  { orderId: "A1", amount: 100 },
  { orderId: "A2", amount: 250 },
  { orderId: "A3", amount: 75 },
  { orderId: "A4", amount: 300 },
];

const totalOrderAmount = orders.reduce((acc, order) => acc + order.amount, 0);

console.log(totalOrderAmount); // 725

문제 3: 데이터 그룹화 (중급)

목표:

  • category별로 제품을 그룹화하고, 각 카테고리의 제품 이름 배열을 만드세요.
  • category가 없는 제품은 "Uncategorized" 그룹에 포함하세요.
const products = [
  { name: "Laptop", category: "Electronics", price: 1000 },
  { name: "Shirt", category: "Clothing", price: 20 },
  { name: "Phone", category: "Electronics", price: 500 },
  { name: "Jeans", price: 50 },
];

const productsByCategory = products.reduce((acc, product) => {
  const category = product.category || "Uncategorized";
  if (!acc[category]) {
    acc[category] = [];
  }
  acc[category].push(product.name);
  return acc;
}, {});

console.log(productsByCategory);
/*
{
  Electronics: ["Laptop", "Phone"],
  Clothing: ["Shirt"],
  Uncategorized: ["Jeans"]
}
*/

문제 4: 데이터 정렬 및 변환 (중급)

목표:

  • score를 기준으로 학생들을 내림차순으로 정렬하세요.
  • 각 학생 객체에 rank 속성을 추가해 순위를 매기세요 (1위부터 시작).
const students = [
  { name: "Eve", score: 85 },
  { name: "Frank", score: 92 },
  { name: "Grace", score: 78 },
  { name: "Helen", score: 95 },
];

// 1. score 기준 내림차순 정렬
const sortedStudents = [...students].sort((a, b) => b.score - a.score);

// 2. rank 속성 추가
sortedStudents.forEach((student, index) => {
  student.rank = index + 1;
});

console.log(sortedStudents);
/*
[
  { name: "Helen", score: 95, rank: 1 },
  { name: "Frank", score: 92, rank: 2 },
  { name: "Eve", score: 85, rank: 3 },
  { name: "Grace", score: 78, rank: 4 }
]
*/

문제 5: 중첩 데이터 처리 (중급)

목표:

  • "Python" 스킬을 가진 직원만 필터링하고, 그들의 nameskills를 새로운 배열로 만드세요.
const employees = [
  { id: 1, name: "John", skills: ["JavaScript", "Python"] },
  { id: 2, name: "Jane", skills: ["React", "Node.js"] },
  { id: 3, name: "Jim", skills: ["Python", "Django"] },
];

const pythonDevs = employees
  .filter((employee) => employee.skills.includes("Python"))
  .map(({ name, skills }) => ({ name, skills }));

console.log(pythonDevs);
/*
[
  { name: "John", skills: ["JavaScript", "Python"] },
  { name: "Jim", skills: ["Python", "Django"] }
]
*/

중복 제거

배열에서 중복된 값을 제거하세요.

const numbers = [1, 2, 2, 3, 4, 4, 5];
const uniqueNumbers = [...new Set(numbers)];
console.log(uniqueNumbers); // [1, 2, 3, 4, 5]

중첩 배열 평탄화

배열의 중첩된 구조를 한 단계로 펼치세요.

const nestedArray = [1, [2, 3], [4, [5, 6]]];
const flatArray = nestedArray.flat(2);
console.log(flatArray); // [1, 2, 3, 4, 5, 6]