0

Given: I have an existing JSON file with some nested comment arrays.

Objective: add some new comments to the nested array and then save the file.

Here is an example of the JSON file with the nested array:

            "projectcategory" : "landscape",    
            "photography": "landscape1","landscape2","landscape3","landscape4"],
            "comments": [
                        {"username": "PhotoDude",
                         "dateposted": "10/12/2018",
                         "usercomment": "These landscape photos are amazing."                    
                        },
                        {"username": "PictureLady",
                         "dateposted": "03/03/2019",
                         "usercomment": "These landscape photos have such great detail."                     
                        }
                      ]

And here is the code I am working with:

     async addEntry(username, dateposted, usercomment, projectcategory) {        
         const data = await this.getData();  
         console.log(data);
         const project = data.find((project) => {           
             return project.projectcategory === projectcategory;
         });

         if(!project || !project.comments) return null;
         project.comments.push({username, dateposted, usercomment});

         return writeFile(this.datafile, JSON.stringify());
      }
Wyck
  • 10,311
  • 6
  • 39
  • 60
jaydog
  • 1

0 Answers0