하루를살자

JS Clone Project [to-do list] - 4, Deleting to-Do list 본문

JS

JS Clone Project [to-do list] - 4, Deleting to-Do list

Kai1996 2021. 12. 9. 16:20

Key Points 

- Logic of deleting selected item on the array  

- Use of filter()

 

function deleteTodo(event){
    //We can identify which li called this event by inspecting event.target object. 
    console.log(event.target);
    console.dir(event.target.parentElement.innerText);

    //I designate the selected li element 
    const li = event.target.parentElement;


    //Remove the list
    li.remove();

    
    toDos = toDos.filter((item) => item.id !== parseInt(li.id));
    saveToDos();
}

Things learned 

- filter() returns creating new array containing element is true for the argument, in this case, "item.id !== li.id". 

- Remember to save the new array into the local storage after creating new toDos array. 

 

Comments