반복문 내장함수(응용)1

1) 실습과제

const items = [
    {
        id: 1,
        name: '아이언맨'
    },
    {
        id: 2,
        name: '캡틴 아메리카'
    },
    {
        id: 3,
        name: '토르'
    },
    {
        id: 4,
        name: '닥터 스트레이지'
    }
];
const jsonadd = function(props, adds) {    //JSON 코드에 data(원소들)밑에 추가하는 기능
    items.forEach((data) => {
        if(data.id == 2){
            data[props] = adds;
            return false;
        }
    });
};
jsonadd('strint', 'love')
jsonadd('lite', 'triplexlab')
// console.log(items);

const jsonupdate = function(id, names){  //JSON 코드에 data(원소들)를 수정하는 기능
    items.forEach((data) => {
        if (data.id == 2) {
            data.name = names;
            return false;
        }
    });
};
jsonupdate(2, 'thdbsgh3443');
// console.log(items);

const jsondelete = function(id, names){  //JSON 코드에 data(원소들)를 삭제하는 기능
    items.forEach((data) => {
        if(data.id == 2){
            delete data.name;
            return false;
        }
    });
};
jsondelete(2, 'thdbsgh344')
// console.log(items);

Last updated