vs코드에서 'ctrl + 클릭' api 정의문서를 읽을수있습니다.
const arr1 = new Array();
const arr2 = [1, 2];
const fruits = ['🍎', '🍌'];
console.log(fruits);
console.log(fruits.length);
console.log(fruits[0]);
console.log(fruits[1]);
console.log(fruits[2]);
console.log(fruits[fruits.length - 1]); // length-1을 사용해서 배열의 마지막 요소에 접근 가능
console.clear();
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
for (let fruit of fruits) {
console.log(fruit);
}
fruits.forEach((fruit) => console.log(fruit));
//파라미터로 value, index, array 세개가 들어갑니다.
push
add an item to the end
fruits.push('🍓', '🍑');
console.log(fruits);
pop
remove an item from the end
const poped = fruits.pop();
fruits.pop();
console.log(fruits);
unshift
add an item to the begining
fruits.unshift('🍓', '🍋');
console.log(fruits);
shift
remove an item from the begining
fruits.shift();
fruits.shift();
console.log(fruits);
splice
remove an item by index position
fruits.push('🍓', '🍑', '🍋');
console.log(fruits);
fruits.splice(1, 1);
console.log(fruits);
fruits.splice(1, 0, '🍏', '🍉'); 지운 자리에 사과와 수박을 넣어줘~
console.log(fruits);
concat
combine two arrays
const fruits2 = ['🍐', '🥥'];
const newFruits = fruits.concat(fruits2);
console.log(newFruits);
shift 당겨오다
중요) shift, unshift 는 pop, push 보다 훨씬훨씬 느립니다.
맨 앞에서 인덱스를 밀고 가져오고 난리를 치기 때문입니다.
splice = 꼬아서 잇다
시작하는 인덱스 번호를 지정해주고, 몇개나 지울지 알려줘야합니다.
몇개나 지울지 안알려주면 지정한 인덱스부터 모든 데이터를 다 지워버립니다.
indexOf
find the index
몇번째 인덱스에 있는지 찾아보기
console.clear();
console.log(fruits);
console.log(fruits.indexOf('🍎'));
console.log(fruits.indexOf('🍉'));
console.log(fruits.indexOf('🥥'));
includes
배열안에 있는지 없는지 true 혹은 false로 알려준다.
배열안에 해당하는 값이 없으면 - 1을 출력한다.
console.log(fruits.includes('🍉'));
console.log(fruits.includes('🥥'));
lastIndexOf
배열안에 해당하는 값이 없으면 - 1을 출력한다.
console.clear();
fruits.push('🍎');
console.log(fruits);
console.log(fruits.indexOf('🍎')); // 제일 첫번째로 들어있는 인덱스를 알려줍니다.
console.log(fruits.lastIndexOf('🥥')); // 제일 마지막에 들어있는 인덱스를 알려줍니다.
{
const fruits = ["apple", "banana", "orange"];
let 문자열 = fruits.join("");
console.log(문자열);
}
배열.join(구분자)
seperator = 한국어로 구분자
{
const fruits = "🍎, 🥝, 🍌, 🍒";
fruits.trim();
배열 = fruits.split(", ");
console.log(배열);
}
문자열.trim()
Removes the leading and trailing white space and line terminator characters from a string.
문자열에서 선행 및 후행 공백과 줄 끝 문자를 제거합니다.
문자열.split()
Split a string into substrings using the specified separator and return them as an array.
지정한 구분 기호를 사용하여 문자열을 하위 문자열로 분할한 후 배열로 반환합니다.
{
const array = [1, 2, 3, 4, 5];
array.reverse(); // 배열자체의 순서를 뒤집어줍니다.
console.log(array);
}
배열.reverse()
배열자체의 순서를 뒤집어줍니다.
{
const array = [1, 2, 3, 4, 5];
const 배열 = array.slice(2, array.length);
console.log(배열); //[ 3, 4, 5 ]
}
배열.slice()
배열.slice(시작인덱스부터, 이 인덱스 전까지)
const array = [1, 2, 3, 4, 5]; array.splice(2, array.length); console.log(array); //[1,2]
splice()는 배열을 직접 잘라버려서 사용할 수 없습니다.
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),
];
{
const result = students.find((student) => student.score === 90);
console.log(result);
}
배열.find((item)=>{조건식})
배열에 있는 모든 요소들을 순회한다.
배열에서 true를 첫번째로 만나면 바로 끝.
{
const result = students.filter((student) => student.enrolled);
console.log(result);
}
배열.filter((item)=>{조건식})
true혹은 false를 반환하는데, true인 요소들만 전달한다.
// result should be: [45, 80, 90, 66, 88]
{
let 배열 = [];
students.map((학생) => {
학생.score;
});
console.log(배열);
}
배열.map((item)=>{가공하기})
배열안에 있는 요소들을 원하는 함수로 가공해서 다른 방식의 데이터로 만들고 싶을때
{
const result = students.some((student) => student.score < 50);
console.log(result);
const result2 = !students.every((student) => student.score >= 50);
console.log(result2);
}
배열.some((item)=>{조건식})
조건이 맞는 놈이 하나라도 있다면 true를 배출한다.
배열.every((item)=>{조건식})
배열에 있는 모든 요소들이 조건에 맞아야 true를 배출한다.
{
// 학생들의 점수를 평균내주세요
const result = students.reduce((prev, curr) => prev + curr.score, 0);
console.log(result / students.length);
}
reduce((이전값,지금값)=>{가공하기})
배열안에 들어있는 모든 요소들을 순회함.
배열에 있는 모든 요소들의 값을 누적할때 쓰는것.
예시
let result = array.reduce((이전값, 지금값, ?인덱스) => {
console.log('-----------');
console.log(이전값);
console.log(지금값); // 배열의 요소 하나하나
return 이전값 + 지금값; // 여기서 return한 값이 다음 반복때 이전값이됨
}, 0 ) 초기값
{
// result should be: '45, 80, 90, 66, 88'
const result = students
.map((student) => student.score) // map은 가공해서 새로운 배열을 리턴해줍니다.
.filter((score) => score >= 50) // 만약 50점 이하인 친구들만 하고싶다면?
.join();
console.log(result);
}
{
// result should be: '45, 66, 80, 88, 90'
const result = students
.map((student) => student.score)
.sort((a, b) => b - a)
.join();
console.log(result);
}