TO DO List
TO DO List ๋ง๋ค๊ธฐ
<div>
<form class="js-to-do to-do">
<input class="js-add-to-do to-do__add-to-do" placeholder="Write a To Do here" type="text" />
</form>
<ul class="js-list list"></ul>
</div>
const form = document.querySelector(".js-to-do"),
input = document.querySelector(".js-add-to-do"),
list = document.querySelector(".js-list");
let toDos = [];
function persistToDos() {
const stringToDo = JSON.stringify(toDos);
localStorage.setItem("toDos", stringToDo);
}
function saveToDo(text) {
const toDoObject = {
id: toDos.length + 1,
value: text
};
toDos.push(toDoObject);
persistToDos();
}
function handleDelete(event) {
const target = event.target;
const li = target.parentElement;
const ul = li.parentElement;
const toDoId = li.id;
ul.removeChild(li);
toDos = toDos.filter(function(toDo) { // filter ๋ชจ๋ ์์ดํ
์ ํตํด ํจ์๋ฅผ ์คํํ๊ณ ture์ธ ์์ดํ
๋ง ๊ฐ์ง๊ณ ์๋ก์ด array๋ฅผ ๋ง๋ฌ
return toDo.id !== parseInt(toDoId); // ๋ชจ๋ toDos๊ฐ li ์ id ์ ๊ฐ์ง ์์๋
});
persistToDos();
}
function addToDo(text) {
const toDo = document.createElement("li");
toDo.className = "toDo";
toDo.id = toDos.length + 1;
const deleteBtn = document.createElement("span");
deleteBtn.innerHTML = "โ";
deleteBtn.className = "toDo__button";
deleteBtn.addEventListener("click", handleDelete);
const label = document.createElement("label");
label.innerHTML = text;
toDo.appendChild(deleteBtn);
toDo.appendChild(label);
list.appendChild(toDo);
saveToDo(text);
}
function onSubmit(event) {
event.preventDefault();
const value = input.value;
input.value = "";
addToDo(value);
}
function loadToDos() {
const loadedToDos = localStorage.getItem("toDos");
if (loadedToDos !== null) {
const parsedToDos = JSON.parse(loadedToDos);
parsedToDos.forEach(function(toDo) { // list์ ์๋ ๋ชจ๋ ltem์ ์ํ ํจ์๋ฅผ ์คํ์ํด
addToDo(toDo.value);
});
}
return;
}
function init() {
loadToDos();
}
form.addEventListener("submit", onSubmit);
init();
filter
๊ธฐ๋ณธ์ ์ธ ๋ฐฉ์
let newArray = arr.filter(callbackFunction(element, index, array), thisArg);
์์
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6); // ๋จ์ด๊ฐ 6๊ฐ ์ด์์ธ element๋ฅผ ์ฐพ์์ค
console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
function isBigEnough(value) {
return value >= 10; // value ๊ฐ์ด 10๋ณด๋ค ํฐ๊ฒ
}
let filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered ๋ [12, 130, 44]
// ์ ์ ๋ฐฐ์ด์์ 5์ ๋ฐฐ์์ธ ์ ์๋ง ๋ชจ์ผ๊ธฐ
var arr = [4, 15, 377, 395, 400, 1024, 3000];
var arr2 = arr.filter(function (n) {
return n % 5 == 0; // 5๋ก ๋๋ด์๋ ๋๋จธ์ง๊ฐ 0 ์ด๋๋๊ฐ
});
console.log(arr2); // [15, 395, 400, 3000]
forEach
๋ฐฐ์ด์ ๋ชจ๋ ์์๋ฅผ ๋ฐ๋ณตํ๋ฉฐ ์ฝ๋ฐฑ ํจ์๋ฅผ ์คํ
๊ธฐ๋ณธ์ ์ธ ๋ฐฉ์
arr.forEach(callback(item, index, array)
์์
const arr = [1,3,5,7,9];
// e๋ element i๋ index์ i,a๋ array
// ์ฝ๋ฐฑํจ์์ ๋งค๊ฐ๋ณ์ ์ด๋ฆ์ ์์ ํด๋ ๋์ง๋ง ์์๋ ๋ณํ์ง ์์ต๋๋ค.
arr.forEach(function(e, i, a) {
console.log('์ธ๋ฑ์ค '+i+ ': ' +e+', ๋ฐฐ์ด์ ์ ์ฒด๋: '+a);
})
//๊ฒฐ๊ณผ๊ฐ
์ธ๋ฑ์ค 0: 1, ๋ฐฐ์ด์ ์ ์ฒด๋: 1,3,5,7,9
์ธ๋ฑ์ค 1: 3, ๋ฐฐ์ด์ ์ ์ฒด๋: 1,3,5,7,9
์ธ๋ฑ์ค 2: 5, ๋ฐฐ์ด์ ์ ์ฒด๋: 1,3,5,7,9
์ธ๋ฑ์ค 3: 7, ๋ฐฐ์ด์ ์ ์ฒด๋: 1,3,5,7,9
์ธ๋ฑ์ค 4: 9, ๋ฐฐ์ด์ ์ ์ฒด๋: 1,3,5,7,9
const arr = [1,3,5,7,9];
arr.forEach((e, i, a)=>{
if (e === 5) return; // return true, false ๋ ๊ทธ๋ฅ continue
console.log('์ธ๋ฑ์ค '+i+ ': ' +e+', ๋ฐฐ์ด์ ์ ์ฒด๋: '+a);
})
//๊ฒฐ๊ณผ๊ฐ
// ์ธ๋ฑ์ค 5 ํด๋นํ๋ ๋ด์ฉ ๋นผ๊ณ ๋ชจ๋ ์ถ๋ ฅ
Last updated
Was this helpful?