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 = 11이 accumulator가 됨accumulator === 1, current === 2가 됨 ⇒ 1 + 2 = 3 .... 각 배열 원소를 다 쓸 때까지 돔accumulator === 10, current === 5 => accumulator + current면 15const 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 }
소수점 뒤에 나타날 자릿수
0 이상 100 이하의 값을 사용
구현체에 따라 더 넓은 범위의 값을 지원할 수도 있음.
값을 지정하지 않으면 0을 사용합니다.
숫자를 고정 소수점 표기법으로 표기해 반환
소수점 이하가 길면 숫자를 반올림하고, 짧아서 부족할 경우 뒤를 0으로 채움
메서드를 호출한 숫자의 크기가 1e+21보다 크다면 Number.prototype.toString()을 호출하여 받은 지수 표기법 결과를 대신 반환