JS/[드림코딩] 엘리 (완)

09강. Array APIs

web_seul 2020. 7. 20. 01:41
반응형

Q1. make a string out of an array

{
  const fruits = ['apple', 'banana', 'orange'];
  const resule = fruits.join();

  console.log(result); //apple.banana,orange


  const resule = fruits.join('^'); //구분자 추가

  console.log(result); //apple^banana^orange
}

 

Q2. make an array out of a string

{
  const fruits = '🍎, 🥝, 🍌, 🍒';
  const result = fruits.split(','); //구분자 전달하기(필수)

  console.log(result); //["🍎","🥝","🍌","🍒"]


  const result = fruits.split(',',2); //앞에서 두개의 인자 전달

  console.log(result); //["🍎","🥝"]
}

 

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] 
}

 

Q4. make new array without the first two elements

{
  const array = [1, 2, 3, 4, 5];
  const result = array.splice(0,2); //배열자체 수정

  console.log(result); //[1,2]
  console.log(array); //[3,4,5]  x


  const result = array.slice(2,5); //배열에서 원하는부분만 return

  console.log(result); //[3,4,5]
  console.log(array); //[1,2,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),

];

 

Q5. find a student with the score 90

{

    const result = students.find(function(student, index){
        console.log(student, index); //학생배열
        return student.score ===90; //true를 찾을때까지 배열을 돔
    }); 

    console.log(result); //student {name:"C", age: 30, enrolled: true, score: 90}

    //간략히
    const result = students.find((student)=> student.score === 90);

}

Q6. make an array of enrolled students

{
    const result = students. filter((student) => student.enrolled);
    console.log(result); //true인 a,c,e 출력
}

Q7. make an array containing only the students' scores _result should be: [45, 80, 90, 66, 88]

//mapping 배열내부 요소에서 대체 

{
    const result = students.map((student) => student); //직관적인 value쓰기
    console.log(result); //[student,student,student,student,student]

    const result = students.map((student) => student.score);
    console.log(result); //[45,80,90,66,88]

    const result = students.map((student) => student.score*2);
    console.log(result); //[90,160,180,132,176]
}

 Q8. check if there is a student with the score lower than 50

{
    const result = students.some((student => student.score < 50));
    console.log(result); //true 하나라도 조건에 만족될 경우 true 출력

    const result = students.every((student => student.score < 50));
    console.log(result); //false 모든값이 조건에 만족될 경우 true 출력

    const result = !students.every((student => student.score < 50));
    console.log(result); //false true의 !로 false 출력
}

Q9. compute students' average score

{
    const result = students.reduce((prev, curr) => {//reduce: 값을 하나씩 누적

        console.log(prv); //이전return값 순차적 전달
        console.log(curr); //배열 item 순차적전달

        return prev + curr.score; //순차적으로 누적된 값
    },0);

    console.log(result); //369;

    //간략히
    const result = students.reduce((prev,curr) => prev +curr.score,0);
    console.log(result/students.length); //73.8;

    const result = students.reduceRight((prev, curr) => {//reduceRight: 배열의 끝부터 값을 하나씩 누적
    },0);
    
    console.log(result); //369;
}

Q10. make a string containing all the scores _result should be: '45, 80, 90, 66, 88'

{
    const result = students.map(student => student.score)//학생들의 배열을 점수로 변환
    console.log(result); //[45,80,90,66,88]

    const result = students.map(student => student.score).join();//string으로 변환
    console.log(result); //45,80,90,66,88

    const result = students
        .map(student => student.score) //배열을 점수로 변환
        .filter(score => score>=50) //50이상 filter
        .join(); //string으로 변환

    console.log(result); //80,90,66,88
}

 

Bonus! do Q10 sorted in ascending order _result should be: '45, 66, 80, 88, 90'

{
    const result = students.map(student => student.score)
        .sort((a,b)=> a-b) //결과값이 -면 a가 b보다 작다
        .join(); //string으로 변환

    console.log(result); //45,66,80,88,90


    const result = students.map(student => student.score)
        .sort((a,b)=> b-a) //큰값이 먼저 배열
        .join(); //string으로 변환

    console.log(result); //90,88,80,66,45
}

 

반응형

'JS > [드림코딩] 엘리 (완)' 카테고리의 다른 글

11강. Callback  (0) 2020.07.21
10강. JSON  (0) 2020.07.20
08강. Array  (0) 2020.07.11
07강. object  (0) 2020.07.05
06강. class vs object, 객체지향 언어 클래스 정리  (0) 2020.07.02