Frontend

5.15 youtube Ellie의 js 강의 7-10(Array, Array문제, json)

단점이없어지고싶은개발자 2021. 5. 16. 02:07
반응형

-Array

//object는 토끼와 당근이면 비슷한 object를 묶어놓는 것을 자료구조라고 한다.

//자료구조와 알고리즘을 공부해야 한다.

'use strict';

 

//Array

 

//1. Declaration

const arr1 = new Array();

const arr2 = [12];

 

//2. Index position

const frtuits = ['사과''바나나'];

console.log(frtuits);

console.log(frtuits.length);

console.log(frtuits[0]);

console.log(frtuits[1]);

console.log(frtuits[2]); //undifined

console.log(frtuits[frtuits.length - 1]); //마지막 배열의 끝에 접근 할 수 있다

 

//3. Looping over an array

//for

for ( let i = 0i < frtuits.lengthi++) {

    console.log(frtuits[i]);

}

 

//for of 변수를 선언 해주고 of 뒤에 변수에 있는 것들을 넣어준다

for(let fruit of frtuits) {

    console.log(fruit);

}

 

//forEach 배열안에 있는 value마다 출력 가능하다

frtuits.forEach((frtuits=> console.log(frtuits));

 

//4. Addtion, deletion, copy

//push : add an item to tne end 뒤에서부터 추가

frtuits.push('dagg''adg');

console.log(frtuits);

 

//pop : 삭제 가장 뒤에께 삭제된다

frtuits.pop();

frtuits.pop();

console.log(frtuits);

 

//unshift : 앞에서 넣는 것

frtuits.unshift('efef''efefewq');

 

//shift : 앞에서부터 뺀다

fruit.shift();

 

//note!! shift, unshift는 pop, push보다 훨씬 느리다

//뒤에서 넣는 것은 아무것도 없는 공간에 뒤에다가 추가, 삭제가 원활하다

//앞에서부터 넣는 것은 데이터를 뒤로 옮기고 빈 공간에 추가와 삭제를 해야하기 때문이다.

 

//splice : remove an item by index position

frtuits.push('fwf''efqq''qfqqqq');

console.log(frtuits);

frtuits.splice(2,4); //데이터 추가도 된다.

 

//concat : combine two arrays 데이터를 붙여줄 수 있다.

 

//5. Searching

//indexOf: find the index

console.log(frtuits);

console.log(frtuits.indexOf('qwd'));//indexOf에서 찾을 수있다. 값이 없다면 -1로 출력한다

 

//lastIndexOf : 뒤에서 부터 찾아준다.

//

console.log(frtuits);

 


-Array 문제

// Q1. make a string out of an array

{

  const fruits = ['apple''banana''orange'];

  const result = fruits.join(); //join으로 string으로 바꿔줄 수 있다. join(구분자) 구분자를 넣어줘서 바꿀 수 있다.

  console.log(result);

}

 

// Q2. make an array out of a string

{

  const fruits = '🍎, 🥝, 🍌, 🍒';

  const result = fruits.split(','); //단위는 ,(콤마) 자른다

  console.log(result);

}

 

// Q3. make this array look like this: [5, 4, 3, 2, 1]

{

  const array = [12345];

  const result = array.reverse(); //배열 자체를 거꾸로 만든다

  console.log(result);

  console.log(array);

}

 

// Q4. make new array without the first two elements

{

  const array = [12345];

  const result = array.slice(2,5); //index n 부터 end는 제외된다 그러기에 + 1 해야한다

  console.log(result);

  console.log(array);

}

 

class Student {

  constructor(nameageenrolledscore) {

    this.name = name;

    this.age = age;

    this.enrolled = enrolled;

    this.score = score;

  }

}

const students = [

  new Student('A'29true45),

  new Student('B'28false80),

  new Student('C'30true90),

  new Student('D'40false66),

  new Student('E'18true88),

];

 

// Q5. find a student with the score 90

{

  const result = students.find((student=> student.score === 90);

  console.log(result);

}

 

// Q6. make an array of enrolled students

{

  const result = students.filter((students=> students.enrolled);

  console.log(result);

}

 

// Q7. make an array containing only the students' scores

// result should be: [45, 80, 90, 66, 88]

{

  const result = stduents.map((student=> student.score); //배열안에 있는 요소들을 다른 값으로 만들고 싶을 때 map을 사용한다

  console.log(result);

}

 

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

{

  const result = students.some((student=> stduents.score < 50); //배열 만족되는 하나만 있다면 some을 이용 모든 배열조건이 만족하려면 every를 사용하면 된다

  console.log(result);

}

 

// Q9. compute students' average score //어렵다 다시 재차 듣자

{

  const result = students.reduce((prevcurr=> {

    consle.log(prev);

    console.log(curr);

    return curr;

  }, 0);

}

 


-Json 

  JSON은 아직 잘 모르겠다 js부터 하고 그리고 넘어가야 되는 부분이라 js하고 한 번 더 학습이 필요하다 

//JSON

 

//1. Object to JSON

//stringfy (obj)

let json = JSON.stringify(ture);

console.log(ison);

 

json = JSON.stringify(['apple''banana']);

console.log(json);

 

const rabbit = {

    name: 'tori',

    color: 'white',

    size: null,

    birthDate: new Date(),

    jump: () => {

        console.log('${name} can jump!');

    },

};

 

let json = JSON.stringify(rabbit);

console.log(ison);

 

let json = JSON.stringify(rabbit, ["name"]); //내가 원하는 프로퍼티만 불러올 수 있다.

console.log(ison);

 

let json = JSON.stringify(rabbit, (keyvalue=> { //더 세밀하게 다루고 싶다면 callbackfuction으로 할 수 있다

    console.log('key: ${key}, valude: ${value}');

    return value;

});

 

console.log(ison);

 

//2. JSON to Object

//parse (json)

json = JSON.stringify(rabbit);

const obj = JSON.parse(json);

console.log(obj);

rabbit.jump();

// obj.jump();

 

rabbit.birthDate.getDate()

반응형