Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!