My function getFormData has a forEach inside that is displaying ‘allComments’ array of objects.
When I click submit I want to add name, email and comment to ‘allComments’ and then display it on the HTML like it’s already happening using the forEach.
I have sorted that out but forgot to save, it’s working on my VS Code but still not as I want it exactly because it’s not displaying the new comments, only the existing ones from the array.
I see where you add them to the array in the following line within the getFormData function, but I do not see where you update the DOM with the contents of the array again.
this is how I am updating it but once I click submit it’s keeping the previous ‘user, email and comment’ and adding the last one to them so I have duplicates.
If you do a console log on allComments after you push the new info to it you will see that you do not have duplicates in that array. So the duplicate issue is not in the array.
Once you print something to the DOM it stays there until you delete it. So do you think you can figure out why you are getting duplicates on the page?
You have at a few choices to avoid displaying what appears as duplicates. My recommendations are you either:
Only append the latest comment to the DOM (while still pushing the latest comment to the allComments array.
OR
After pushing the new comment to the allComments array, you would iterate through all the comments dynamically creating an html string and after iterating, assign this html string to the innerHTML property of an element containing the comments.