Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
Why is this function always declaring this property as an empty array?

Why is this newProject() function always declaring the tasks property as an empty array []?

When the page loads, the console.log(JSON.parse(localStorage.getItem(`${name}`))) is null so the tasks will be declared as an empty array, however when I refresh the page, the console.log(JSON.parse(localStorage.getItem(`${name}`))) is not null anymore, so the tasks should be declared as (JSON.parse(localStorage.getItem(`${name}`))) and not an empty array


export const allProjects = JSON.parse(localStorage.getItem("allProjects")) ?? [];


export function newProject(name) {

    console.log(JSON.parse(localStorage.getItem(`${name}`))) 
    // Null, when opening page for the first time

    allProjects.push({
        projectTitle: name,
        id: crypto.randomUUID(),
        tasks: JSON.parse(localStorage.getItem(`${name}`)) ?? []
    })
    getProjectId(name)

    localStorage.setItem("allProjects", JSON.stringify(allProjects))
}

newProject("Default")
newProject("random stuff")

export function newTask(projectTitle, title, desc, dueDate, priority) {
    getProjectId(projectTitle)
    project.tasks.push({
        title: title,
        id: crypto.randomUUID(),
        desc: desc,
        dueDate: dueDate,
        priority: priority
    })
    getTaskId(title)

    localStorage.setItem(project.projectTitle, JSON.stringify(project.tasks))
    localStorage.setItem(title, JSON.stringify(task))
}

newTask("Default", "first task", "desc", "due Date", "urgent")
newTask("Default", "second task", "desc", "due date", "no urgent")


// GET ID FUNCTIONS 

export let projectId;
export let project;

export function getProjectId(projectName) {
    project = allProjects.find(obj => obj.projectTitle === projectName)
    projectId = project.id;
    return {
        projectId,
        project
    }
}

export let task;
export let taskId;
export let altTask;

export function getTaskId(taskName) {
    
    
    task = project.tasks.find(task => task.title === taskName)
    altTask = task;
    taskId = task.id;
    
    return task, taskId
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda