JavaScript 공부 [8. 배열 API]

Array APIs

join

// Q1. make a string out of an array
{
  const fruits = ['apple', 'banana', 'orange'];
}
// Answer
{
  const result = fruits.join()
  console.log(result)			// apple,banana,orange
  const result = fruits.join(' ')
  console.log(result)			// apple banana orange
}

split

// Q2. make an array out of a string
{
  const fruits = '🍎, 🥝, 🍌, 🍒';
}
// Answer
{
  const result = fruits.split(',')
  console.log(fruits)			// ["🍎", "🥝", "🍌", "🍒"]
  const result = fruits.split(',', 2)
  console.log(fruits)			// ["🍎", "🥝"]
  const result = fruits.split()
  console.log(fruits)			// ["🍎, 🥝, 🍌, 🍒"]
}

reverse

// Q3. make this array look like this: [5, 4, 3, 2, 1]
{
  const array = [1, 2, 3, 4, 5];
}
// Answer
{
  const result = array.reverse()
  console.log(result)			// [5, 4, 3, 2, 1]
  console.log(array)			// [5, 4, 3, 2, 1]
}

slice

// Q4. make new array without the first two elements
{
  const array = [1, 2, 3, 4, 5];
}
// Answer
{
  const result = array.slice(2, 5)
  console.log(result)			// [3, 4, 5]
}

find

class Student {
  constructor(name, age, enrolled, score) {
    this.name = name;
    this.age = age;
    this.enrolled = enrolled;
    this.score = score;
  }
}
const students = [
  new Student('A', 29, true, 45),
  new Student('B', 28, false, 80),
  new Student('C', 30, true, 90),
  new Student('D', 40, false, 66),
  new Student('E', 18, true, 88),
];


// Q5. find a student with the score 90
{
}
// Answer
{
  const result = students.find((student) => student.score == 90)
  console.log(result)			// Student('C', 30, true, 90)
}

filter

// Q6. make an array of enrolled students
{
}
// Answer
{
  const result = students.filter((student) => student.enrolled)
  console.log(result)
}

map

// Q7. make an array containing only the students' scores
// result should be: [45, 80, 90, 66, 88]
{
}
// Answer
{
  const result = students.map((student) => student.score)
  console.log(result)
}

some

// Q8. check if there is a student with the score lower than 50
{
}
// Answer
{
  // 한명이라도 50점 미만인 학생이 있니?
  const result = students.some((student) => student.score < 50)
  console.log(result)			// true
  
  // !(모든 학생이 50점 이상이니?)
  const result2 = !students.every((student) => student.score >= 50)
  console.log(result2)			// true
}

reduce

배열을 시작부터 끝까지 돌면서 어떠한 값을 누적할 때 사용!

// Q9. compute students' average score
{
}
// Answer
{
  const result = students.reduce((prev, curr) => prev + curr.score, 0)
  console.log(result / students.length)
}

map, filter, join

// Q10. make a string containing the scores higher than 50
// result should be: '80, 90, 66, 88'
{
}
// Answer
{
  const result = students
    .map((student) => student.score)	// Student Object에서 score만 추출한 array
    .filter((score) => score >= 50)	// score만 남아있다.
    .join()				// string 만들기
  console.log(result)
}



// Bonus! do Q10 sorted in ascending order
// result should be: '45, 66, 80, 88, 90'
{
}
// Answer
{
  const result = students
    .map((student) => student.score)	// Student Object에서 score만 추출한 array
    .sort((a, b) => a - b)		// 오름차순 정렬. 내림차순을 하고싶다면 b - a
    .join()				// string 만들기
  console.log(result)
}

좋은 웹페이지 즐겨찾기