
// Array
var arr1 = [1, 2, 3, 4, 5];
var arr2 = [...arr1, 6, 7, 8, 9];
console.log(arr2); // [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
// String
var str1 = 'paper block';
var str2 = [...str1];
console.log(str2); // [ "p", "a", "p", "e", "r", " ", "b", "l", "o", "c", "k" ]
const fruits = ["Apple", "Banana", "Cherry"];
const result = fruits.join();
document.writeln(result); // "Apple,Banana,Cherry"
‼️ ∴ 구분자로 빈 문자열("")을 사용하면 배열의 요소들이 연속된 하나의 문자열로 합쳐짐
// 문자열 my_string이 매개변수로 주어집니다. my_string을 거꾸로 뒤집은 문자열을 return하도록 solution 함수를 완성해주세요.
const solution = (my_string) => {
return [...my_string].reverse().join("");
}
// 일반적인 함수
function key() {
return 'abc123'
}
// 화살표 함수
const key = () => {
return 'abc123'
}
const formatUser = name => `${capiitalize(name)}님이 로그인하셨습니다`;
//예시 코드
const team = [
{
name: 'a',
position: 'designer'
},
{
name: 'c',
position: 'pm'
},
{
name: 'b',
position: 'manager'
},
{
name: 'd',
position: 'developer'
},
{
name: 'e',
position: 'developer'
}
]
const scorces = [30, 82, 70, 45];
const getNumberOfPassingScorces = (scorces) => {
const passing = scorces.filter(scorces => scorcess > 59);
return passing.length;
}
+) 특정 배열 메서드를 호출하고, 반환된 결과는 이어지는 배열 매서드로 전달해서 사용도 가능
+) parseFloat :
//예시
const prices = ['1.0', '흥정가능', '2.15'];
const formateedPrices = prices.map(price => parseFloat(price)).filter(price => price);
// [1.o, 2.15]
//band라는 배열은 name, instrument가 있는 객체들의 배열
const instruments = band.map(member => member.instrument);
// 이름에 'Dav'가 들어가는 사람만 분류해내기
const team = ["Michelle B", "Dave L", "Dave C", "Courtney B", "Davina M"];
const daves = team.filter(member => member.match(/Dav/));
// sailingClub 배열에 사람들 이름이 있는 경우
// 이메일을 보내는 코드는 다음과 같음
sailingClub.forEach(member => sendEmail(member));
/*
문제 설명
- sailors라는 배열은 name, active(활성 여부), email 객체를 가지고 있는 배열
- active 상태인 회원들에게만 이메일을 보내는 코드를 작성하라
*/
// 활성화 상태인 유저들 배열로 뽑기
const activeSailors = sailors.filtor(sailor => sailor.active);
// 활성화 유저들의 이메일 가져오기
const emails = activeSailors.map(sailor => sailor.email);
// 이메일 전송 함수로 이메일들 발송
emails.forEach(email => sendEmail(email));
위와 같은 단계들을 거쳐서 값을 가져올 수 있음.
이걸 이제 체이닝으로 연결시키면 다음과 같음.
sailors
.filter(sailor => sailor.active)
.map(sailor => sailor.email)
.forEach(sailor => sendEmail(sailor));

const numbers = [1,2,3,4];
// Case1. forEach를 사용
let total = 0;
numbers.forEach(number => {
total += number
});
// Case 2. reduce를 사용
const total = numbers.reduce((accumulator, currentValue) => {
// 이렇게 반환된 값은 다시 누적되는 값 (accumulator)에 저장되어서 다음 currentValue에 해당되는 함수를 돌림
return accumulator + currentValue;
}, 0);

const numbers = [10, 4, 2, 8]
// 초기값을 설정 X했으니, 초기값은 배열의 첫번째 값이 됨!
const smallest = numbers.reduce((smallNum, currentValue) => {
if(smallNum > currentValue) {
return currentValue;
}
return smallNum;

const cart = [
{
name: '사과',
price: 500
},
{
name: '바나나',
price: 700
},
{
name: '레몬',
price: 300
},
];
const total = cart.reduce((accumulator, fruit)=> {
return accumulator += fruit.price;
}, 0);
const solution = (numbers) => {
return numbers.reduce((accumulator, currentValue)=> {
return [...accumulator, currentValue * 2];
}, []);
}
