Tom
Tom

Reputation: 9

Using LocalStorage in JS using vue.js and having a complex set of objects

I use vue.js, I have a JSON called projects which will have inside an array of the object Teams and each Team will have an array of objects User.

Like this: https://project1-e5692.firebaseapp.com/

I can't figured out how to save a new team or a new user successfully on that JSON, I can already Load the main JSON, do something with it and then save that main JSON again.

saveteam() {
    project = JSON.parse(localStorage.getItem('projects'));
    var user1 = {"name":"user1"};  //To test if a user gets  pushed in a team 

    team.push(user1);
    team.teamname=this.teamname; //What I get from the HTML
    projects.push(team);    //Trying to push the team (Wish has the user inside already) on the current project
    console.log(projects);  
    console.log(project);
    project.push(projects);    //then I push a new project inside the projects array
    localStorage.setItem("projects",JSON.stringify(project)); // then I save the JSON back
}

Upvotes: 1

Views: 112

Answers (1)

Ziv Ben-Or
Ziv Ben-Or

Reputation: 1194

saveteam() { 
    let projects = JSON.parse(localStorage.getItem('projects'));
    let project = {};
    let team = {};
    let user1 = {"name":"user1"};  

    team.push(user1);
    project.push(team);  
    projects.push(project);   
    localStorage.setItem("projects",JSON.stringify(projects)); 
},

Upvotes: 2

Related Questions