(@구조분해할당, @배열구조분해, @객체구조분해)
const animal = {
name : '멍멍이',
type : '개'
}
const {name, type} = animal;
console.log(name);
console.log(type);
/* 객체의 깊숙한곳에 있는 값을 꺼내는법 */
const deepObject = {
state : {
information : {
name : 'jibbang',
languages : ['korean', 'english']
}
},
value : 5
}
const {name, languages} = deepObject.state.information;
const {value} = deepObject;
console.log(name);
const animal = ['강아지', '고양이'];
const [dog, cat] = animal;
console.log(dog);
const pet = {
dog : '개',
cat : '고양이'
}
const animal = {
...pet,
tigger : '호랑이'
}
console.log(animal);
const oneTwo = [1, 2];
const number =[...oneTwo, 3, 4];
console.log(number);
const animal = {
dog: '개',
cat: '고양이',
tigger: '호랑이'
}
const {dog, ...rest} = animal;
console.log(dog);
console.log(rest);
const number = [1, 2, 3, 4];
const [one, two, ...rest] = number;
console.log(one);
console.log(two);
console.log(rest);
function total(...rest) {
return rest;
}
const result = total(1, 2, 3, 4);
console.log(result);
(@배열함수, @배열의내장함수, @배열의함수)
const number = [1, 2, 3, 4];
console.log(number.length); // 4
const animals = ['개', '고양이', '토끼', '거북이'];
animals.forEach(animal => {
console.log(animal);
});
// 개, 고양이, 토끼, 거북이 가 각각 출력됨
const number = [1, 2, 3, 4, 5];
const newNumber = number.map(n => n * n);
console.log(newNumber); // [1, 4, 9, 16, 25]
const animals = ['개', '고양이', '거북이', '토끼'];
console.log(animals.indexOf('고양이')); // 1
const todos = [
{
id: 1,
text: 'java',
done: true
},
{
id: 2,
text: 'javascript',
done: true
},
{
id: 3,
text: 'react',
done: true
},
{
id: 1,
text: 'spring',
done: false
}
]
const index = todos.findIndex(todo => todo.id === 3);
console.log(index); // 2
const todos = [
{
id: 1,
text: 'java',
done: true
},
{
id: 2,
text: 'javascript',
done: true
},
{
id: 3,
text: 'react',
done: true
},
{
id: 1,
text: 'spring',
done: false
}
]
const todo = todos.find(todo => todo.id === 3);
console.log(todo); // {id: 3, text: 'react', done: true}
const todos = [
{
id: 1,
text: 'java',
done: true
},
{
id: 2,
text: 'javascript',
done: true
},
{
id: 3,
text: 'react',
done: true
},
{
id: 1,
text: 'spring',
done: false
}
]
const.taskNotDone = todos.filter(todo => todo.done === false);
console.log(taskNotDone); // {id: 4, text: 'spring', done: false}
const numbers = [10, 20, 30, 40];
const index = numbers.indexOf(30);
const spliced = numbers.splice(index, 2); // index부터 2개 지우겠다.
//지운값[30, 40]이 spliced에 저장된다.
console.log(spliced); // [30, 40]
console.log(numbers); // [10, 20] -> 기존 객체가 변경된다.
const numbers = [10, 20, 30, 40];
const sliced = numbers.slice(0, 2); // '0'번 index로부터 '2'번 index전까지만 선택된다.
console.log(sliced); // [10, 20]
console.log(numbers); // [10, 20, 30, 40]
const numbers = [10, 20, 30, 40];
const value = numbers.shift();
console.log(value); // 10
console.log(numbers); // [20, 30, 40]
const numbers = [10, 20, 30, 40];
numbers.unshift(5);
console.log(numbers); // [5, 10, 20, 30, 40]
const numbers = [10, 20, 30, 40];
const value = numbers.pop();
console.log(value); // 40
console.log(numbers); // [10, 20, 30]
const numbers = [10, 20, 30, 40];
numbers.push(50);
console.log(numbers); // [10, 20, 30, 40, 50]
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const array1 = arr1.concat(arr2);
const array2 = arr1.concat(4);
console.log(array1); // [1, 2, 3, 4, 5, 6]
console.log(array2); // [1, 2, 3, 4]
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 = arr.reduce((누적연산값, 현재index의 데이터값) => {
누적연산값 + 현재index데이터
}, 누적연산기본값);
*/
const sum = numbers.reduce((accumulator, current) => accumulator + current, 0);
console.log(sum); // 15
const numbers = [1, 2, 3, 4, 5];
/*
const avg = arr.reduce((누적연산값, 현재index의 데이터값, current의 index, arr배열) => {
연산과정
}, 누적연산기본값);
*/
const avg = numbers.reduce((accumulator, current, index, array) => {
if(index === array.length - 1){
return (accumulator + current) / array.length;
}
return accumulator + current;
}, 0);
console.log(avg); // 3
const alphabets = ['a', 'a', 'a', 'b', 'c', 'c', 'd', 'e'];
const counts = alphabets.reduce((acc, current) => {
if(acc[current]) { // acc['a'] === acc.a
acc[current] += 1; // acc.a += 1
}else {
acc[current] = 1;
}
return acc;
}, {}); // acc의 초기값 {} (빈객체)
console.log(counts); // {a: 3, b: 1, c: 2, d: 1, e: 1}