jsNote 08: Array APIs

Jieun·2021년 1월 11일
0

jsNote

목록 보기
8/13

1. array.join 배열을 문자열로 변환

// Q1. make a string out of an array
{
    const fruits = ['apple', 'banana', 'orange'];
    const result = fruits.join(' and '); //join을 쓰면 string으로 변환 가능
    console.log(result); //apple and banana and orange
}



2. array.split 문자열을 배열로 변환

// Q2. make an array out of a string
{
    const fruits = '🍎, 🥝, 🍌, 🍒';
    const result = fruits.split(',', 3); 
    console.log(result); //["🍎", " 🥝", " 🍌"] 구분자는 필수, limit은 옵션
}



3. array.reverse

// Q3. make this array look like this: [5, 4, 3, 2, 1]
{
    const array = [1, 2, 3, 4, 5];
    const result = array.reverse();
    console.log(result); //[5, 4, 3, 2, 1]
    console.log(array); //[5, 4, 3, 2, 1] 원래 배열 자체도 reverse 된다
}



4. array.slice 배열의 특정 부분 리턴

{
    const array = [1, 2, 3, 4, 5];
    const result = array.slice(2,5); //배열의 특정 부분을 return 
    console.log(result); //[3, 4, 5]
}



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),
];

5. array.find

  • 점수가 90점 이상인 학생 찾기
  • 콜백함수는 boolean 타입을 리턴한다. 최초 true 값 리턴.
// Q5. find a student with the score 90
{
    const result = students.find((student)=> student.score === 90);
    console.log(result); //Student {age: 30, enrolled: true, name: "C", score: 90}



6. array.filter

  • 등록한 학생 배열 만들기
// Q6. make an array of enrolled students
{
    const result = students.filter((student)=> student.enrolled);
    console.log(result);
    //0: Student {name: "A", age: 29, enrolled: true, score: 45}
    //1: Student {name: "C", age: 30, enrolled: true, score: 90}
    //2: Student {name: "E", age: 18, enrolled: true, score: 88}
}



7. array.map

  • 점수만 들어있는 배열 만들기
// Q7. make an array containing only the students' scores
// result should be: [45, 80, 90, 66, 88]
{
    const result = students.map((student)=> student.score); 
    //배열 안에 들어있는 각각의 요소를 새로운 값으로 변환
}



8. array.some, array.every

  • 점수가 50점 이하인 학생이 있는지 체크하기
// Q8. check if there is a student with the score lower than 50
{
    const result = students.some((student) => student.score < 50); //some이라는 API는 조건 충족시 t/f로 출력
    console.log(result); //true

    const result2 = !students.every((student) => student.score > 50); //모든 학생들의 점수가 충족되어야 true 출력
    console.log(result2); //true
}



9. array.reduce

  • 점수의 평균값 구하기
  • reduce는 우리가 원하는 시작점부터 배열에 있는 모든 값을 누적하는 API
// Q9. compute students' average score
// reduceright은 제일 뒤에 있는 값부터 누적
{
    const result = students.reduce((prev, curr) => {
        console.log('---------------------');
        console.log(prev);
        console.log(curr);
        return prev + curr.score;
    }, 0);
    console.log(result/students.length); //73.8
}



10. API 묶어서 이용하기

  • 50점 이상인 점수를 모두 포함한 문자열 만들기
// Q10. make a string containing all the scores
{
    const result = students
    .map((student) => student.score)
    .filter((score) => score >= 50)
    .join();
    console.log(result); //80,90,66,88
}



11. array.sort

  • 점수를 오름차순으로 정렬하기
// do Q10 sorted in ascending order
{
    const result = students.map(student => student.score)
    .sort((a, b) => a - b) 
    //이전값과 현재값 리턴. a에서 b를 뺐을 때 마이너스 값이 나오면 작은 것부터 sorting
    //큰 점수부터 나오기를 원하면 b - a
    .join();
    console.log(result); //45, 66, 80, 88, 90
}



0개의 댓글

관련 채용 정보