const str = "string";
const n = 5;
const result = str.repeat(n);
console.log(result); // 출력: "stringstringstringstringstring"
const superheros = ["아이언맨", "캡틴", "토르", "닥터스크레인지"];
superheros.forEach((hero)=>{
console.log(hero);
});
/* 결과
for ( let i = 0; i < superheros.length; i++) {
console.log(superheros[i])
}
*/
const array = [1, 2, 3, 4, 5, 6, 7, 8];
const sqaured = array.map(n => n* n);
console.log(sqaured); //[1, 4, 9, 16, 25, 36, 49, 64]
const superheros = ["아이언맨", "캡틴", "토르", "닥터스크레인지"];
const index = superheros.indexOf('토르');
console.log(index);
const todos = [
{
id: 1,
text: '자바스크립트 입문',
done: true
},
{
id: 2,
text: '함수 배우기',
done: true
},
{
id: 3,
text: '객체와 배열 배우기',
done: true
},
{
id: 4,
text: '배열 내장함수 배우기',
done: false
},
];
const index = todos.findIndex(todos => todos.id === 3);
console.log(index); //2
//find()는 객체, 원소 그 자체를 출력함
//indexOf, find, findIndex 같은 경우 가장 첫번째로 찾은 그런 항목을 알려준다
const todo = todos.find(todo => todo.done === false);
console.log(todo);
/** 결과
//[object Object]
{
id: 4,
text: '배열 내장함수 배우기',
done: false
}
*/
const taskNotDone = todos.filter(todo => !todo.done);
console.log(taskNotDone);
//[{id:4, text:"배열 내장함수 배우기", done: false}]
배열 내의 특정한 요소를 삭제하거나, 다른 요소로 대치하거나 새로운 요소를 추가할 때 사용
splice(배열의 index의 시작점, 삭제할 요소의 개수, 추가하고 싶은 요소)
댓글 삭제 기능 구현 시 많이 활용 됨
예를 들어, [1, 2, 3, 4, 5]
라는 배열에서 숫자 3을 제거하고 그 자리에 10을 추가하려고 함
const num = [1, 2, 3, 4, 5];
num.splice(2, 1, 10);
console.log(num); // [ 1, 2, 10, 4, 5 ]
const numbers = [10, 20, 30, 40];
const index = numbers.indexOf(30);//몇번째인지 명시
const spliced = numbers.splice(index,2);//index부터 몇개를 지워라
console.log(spliced);//[30, 40]//지운거
console.log(numbers); //[10, 20]//남은거
const numbers = [10, 20, 30, 40];
const slised = numbers.slice(0, 2); //0부터 2전까지 자른다.
console.log(slised); //[10, 20] //자른거
console.log(numbers); //[10, 20, 30, 40]//기존 배열 그대로
const nums = [1, 2, 3, 4, 5];
const nums_new = nums.slice(-2);
console.log(nums) // [1, 2, 3, 4, 5]
console.log(nums_new) // [4, 5]
const numbers = [10, 20, 30, 40];
numbers.unshift(5); //배열 맨 앞에 추가
const value = numbers.shift(); //배열 맨 앞에서 추출, 배열수정
console.log(value); //5 //추출한거
console.log(numbers); //[10, 20, 30, 40] // 남은 배열
const number = [10, 20, 30, 40];
number.push(50); //배열 맨 뒤에 추가
const value = numbers.pop(); // 배열 맨 뒤에서 추출, 배열 수정
console.log(value); //50 //추출한거
console.log(numbers); //[10, 20, 30, 40] //남은 배열
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const arr3 = [7, 8, 9];
const concated = arr1.concat(arr2,arr3);
console.log(concated); //[1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(arr1); //[1, 2, 3]
console.log(arr2); //[4, 5, 6]
console.log(arr3); //[7, 8, 9]
// const concated2 = [...arr1, ...arr2]; //스프레드연산자(ES6)
// console.log(concated2);
const array = [1, 2, 3, 4, 5];
console.log(array.join()); //1,2,3,4,5
console.log(array.join(' ')); // 1 2 3 4 5
console.log(array.join('/ ')); // 1/ 2/ 3/ 4/ 5
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce(
(accumulator, current) => accumulator + current, 0
);
console.log(sum); //15
accumulator
(누적된 값) 0
이, accumulator
가 된다.current
(각 원소들)를 가르키게 되는데 처음엔 1
이 들어가지게 됨0
, 1
이 된다 ⇒ 0 + 1 = 1
1
이 accumulator
가 됨accumulator === 1
, current === 2
가 됨 ⇒ 1 + 2 = 3
.... 각 배열 원소를 다 쓸 때까지 돔accumulator === 10
, current === 5
=> accumulator + current
면 15
const numbers = [1, 2, 3, 4, 5];
const avg = numbers.reduce((accumulator, current, index, array) => {
if(index === array.length - 1){ //조건 맞을때까지 2번 리턴만 실행 // -1하는 이유는 length가 5이고 index가 4이기 때문에 맞추는 것
return (accumulator + current) / array.length; //1번
}
return accumulator + current; //2번
}, 0);
console.log(avg);
index
- 배열의 몇번째 원소인지
array
- 우리가 함수를 실행하고 있는 자기 자신을 의미(numbers
)
const alphabets = ['a', 'a', 'a', 'b', 'c', 'c', 'd', 'e'];
const counts = alphabets.reduce((acc, current) => {
if(acc[current]){
acc[current] += 1;
}else{
acc[current] = 1;
}
return acc;
}, {}); //1.{}:코드블럭 2.{}:빈객체
console.log(counts); //{a:3,b:1,c:2,d:1,e:1}
{}
: 초기엔 빈객체가 acc
,
1. acc[current]
가 a가 아니므로 a= 1
2. acc[current]
가 a니깐 a+1을 함 * 2 = a === 3
3. acc[current]
a가 아니므로 b = 1
4. acc[current]
가 b가 아니므로 c = 1
5. acc[current]
가 c니깐 c+1을 함 = c === 2
6. acc[current]
가 c가 아니므로 d = 1
7. acc[current]
가 d가 아니므로 e = 1
8. 배열이 다 연산되어 출력 { a: 3, b: 1, c: 2, d: 1, e: 1 }
이 글은 패스트캠퍼스 '프론트엔드(React)올인원패키지Online'을 수강하며 정리한 노트입니다.
https://fastcampus.co.kr/search?keyword=%ED%94%84%EB%A1%A0%ED%8A%B8%EC%97%94%EB%93%9C