모던 자바스크립트 - ParkEunwoo/seoul-smart-app GitHub Wiki

let mentorings = [
    {
        id: 1,
        title: '알고리즘',
        place: '무중력지대 대방',
        time: '17:00',
        num: 1,
        total: 10
    },
    {
        id: 2,
        title: '리액트 네이티브',
        place: '무중력지대 양천',
        time: '15:00',
        num: 2,
        total: 12
    },
    {
        id: 3,
        title: '그래프큐엘',
        place: '숭실대학교',
        time: '16:30',
        num: 4,
        total: 6
    }
];
const newMentoring = {
    id: 4,
    title: '자바스크립트',
    place: '무중력지대',
    time: '18:00',
    num: 2,
    total: 3
}
const printFunction = (mtrs) => {
    const show = mtrs.map(({title, place, time, num, total}) => `제목 : ${title} 장소 : ${place} 시간 : ${time} 인원 : ${num}/${total}`);
    console.log(show.join('\n'));
}

printFunction(mentorings);
const addMentoring = (payload, mtrs) => [...mtrs, payload];  //(...)펼치고 추가하고(payload) 다시 배열로 묶기([])
const update = {
    id:2,
    modify:{
        title: '리엑트 네이티브 공부하기',
        time: '16:00'
    }
}
const getMentoringById = (ID, mtrs) => {
    const [mentoring] = mtrs.filter(({id}) => id === ID); // []지우는 방법2 : b=[a] 일때 [b] = [a]면 b=a //filter는 같은 인덱스 걸러내서 반환
    return mentoring;
}
const findIndex = (mentoring, mtrs) => mtrs.indexOf(mentoring);
const modifyMentoring = ({id, modify}, mtrs) => {
    const index = findIndex(getMentoringById(id, mtrs), mtrs);

    return [
        ...mtrs.slice(0, index),
        {...mtrs[index], ...modify},
        ...mtrs.slice(index+1)
    ];
}
const deleteMentoring = (id, mtrs) => {
    const index = findIndex(getMentoringById(id, mtrs), mtrs);

    return [
        ...mtrs.slice(0, index),
        mtrs.slice(index+1)
    ]
}
addMentoring(newMentoring, mentorings);
modifyMentoring(update, mentorings);
deleteMentoring(2, mentorings);