Learn localStorage by Building a Todo App

**Please help me :
In the line : const dataArrIndex = taskData.findIndex((item) => item.id === currentTask.id);
We have not pass any value in this object so how can we use currentTask.id
**

const taskData = JSON.parse(localStorage.getItem("data")) || [];
let currentTask = {};

const addOrUpdateTask = () => {
  const dataArrIndex = taskData.findIndex((item) => item.id === currentTask.id);
  const taskObj = {
    id: `${titleInput.value.toLowerCase().split(" ").join("-")}-${Date.now()}`,
    title: titleInput.value,
    date: dateInput.value,
    description: descriptionInput.value,
  };

  if (dataArrIndex === -1) {
    taskData.unshift(taskObj);
  } else {
    taskData[dataArrIndex] = taskObj;
  }

  localStorage.setItem("data", JSON.stringify(taskData));
  updateTaskContainer()
  reset()
};

It is defined in the global scope so you can use it inside the function.

1 Like

The addOrUpdateTask function does two things, it either adds or updates.

If you add a task and then edit it, currentTask will be the object of that task. When you submit the edit, it calls addOrUpdateTask.

The editTask function doesn’t update the taskData for the task, it updates the DOM elements and sets the currentTask.

1 Like

you haven’t actually used it yet, you are only declaring it inside of your function for when addOrUpdateTask() is going to be called later.,

for this project addOrUpdateTask() is going to be called every time you do something else, by the computer’s running time of “something else”, you will already have indexed data defined in the program.

1 Like