동일한 type의 object를 모아 놓은 것
const arr1 = new Array()
const arr2 = [1, 2]
첫 번째 아이템은
Array[0]
, 마지막 아이템은Array[Array.length -1]
⭐ VScode에서 Ctrl을 눌러 내장함수의 API를 확인할 수 있다. ⭐
배열의 길이가 길 수록 pop / push를 사용하는 게 좋다. unshift / shift는 앞에서부터 실행되기 때문에 배열이 움직이는 시간이 오래 걸린다.
Array.splice(1, 1, 'a','b')
Index 1에서 하나의 아이템을 지우고 a, b 를 추가한다.const newArray = Array.concat(Array2)
아이템의 Index배열 안에 해당하는 값이 없을 때에는 -1이 출력된다.
class Student {
constructor(name, age, enrolled, score) {
this.name = name;
this.age = age;
this.enrolled = enrolled;
this.score = score;
}
}
const student = [
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),
];
// 90점을 받은 학생 찾기
const result = student.find((student) => student.score === 90);
console.log(result); // Student {name: "C", age: 30, enrolled: true, score: 90}
// 등록한 학생들 찾기
const result = student.filter((student) => student. enrolled);
console.log(result); // (3) [Student, Student, Student]
// 배열에서 점수만 가져오기
const result = student.map((student) => student.score);
console.log(result); // (5) [45, 80, 90, 66, 88]
// 50점 이상인 점수만 string으로 가져오기
const result = student
.map((student) => student.score)
.filter((score) => score >= 50)
.join();
console.log(result); // 80, 90, 66, 88
const result = students
.map((student)=> student.score)
.sort((a,b) => a-b)
.join();
console.log(result); // 45, 66, 80, 88, 90
}
// 50점 이하의 학생이 있는지 없는지 확인하기
const result = student.some((student) => student.score < 50));
console.log(result); // true
// 점수의 평균 구하기
const result = student.reduce((prev, curr) => prev + curr.score, 0);
console.log(result / students.length); // 73.8
let json = JSON.stringify(['a','b']);
console.log(json); // ["a", "b"]
const rabbit ={
name: 'tori'
birthDate: new Date(),
jump: () => {
console.log(`${name} can jump!`);
}
}
json = JSON.stringify(rabbit, ['name']);
console.log(json); // {"name":"tori"}
Object를 JSON으로 변환할 때, 함수와 JS에만 있는 dataType은 JSON에 포함되지 않는다.
json = JSON.stringify(rabbit);
const obj = JSON.parse(json);
console.log(rabbit.birthdate.getDate()); // 날짜를 가져온다
console.log(obj.birthDate.getDate()); // String 형태로 저장되었기 때문에 getDate() 함수가 실행되지 않는다.