-Array
//object는 토끼와 당근이면 비슷한 object를 묶어놓는 것을 자료구조라고 한다.
//자료구조와 알고리즘을 공부해야 한다.
'use strict';
//Array
//1. Declaration
const arr1 = new Array();
const arr2 = [1, 2];
//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 = 0; i < frtuits.length; i++) {
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 = [1, 2, 3, 4, 5];
const result = array.reverse(); //배열 자체를 거꾸로 만든다
console.log(result);
console.log(array);
}
// Q4. make new array without the first two elements
{
const array = [1, 2, 3, 4, 5];
const result = array.slice(2,5); //index n 부터 end는 제외된다 그러기에 + 1 해야한다
console.log(result);
console.log(array);
}
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((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((prev, curr) => {
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, (key, value) => { //더 세밀하게 다루고 싶다면 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()
'Frontend' 카테고리의 다른 글
바닐라 Js로 크롬 앱 만들기 #2 PRACTICE (0) | 2021.05.19 |
---|---|
바닐라 Js로 크롬 앱 만들기 #1Theory (0) | 2021.05.18 |
5.14 youtube Ellie의 js 강의 4-6(feat. operator, function, object-class (0) | 2021.05.15 |
5.13 youtube Ellie의 js 강의 1-3 (0) | 2021.05.14 |
5.12 poiemaweb 자바스크립트 공부 //복습필요 (0) | 2021.05.13 |