Currently, I am working on a project where I need to store a value from an input field in an array that is stored in local storage. While I have made progress based on previous questions here, I seem to be facing some issues. After entering some data, I used console.log to check if my array is being populated but it shows nested arrays in the console which I am finding difficult to manage.
This is my JavaScript code:
names = [];
names.push(JSON.parse(localStorage.getItem('locname')));
localStorage.setItem('locname', JSON.stringify(names));
function saveData(data) {
names = [];
var data = document.getElementById("locationName").value;
names = JSON.parse(localStorage.getItem('locname'));
names.push(data);
alert(names);
localStorage.setItem('locname', JSON.stringify(names));
}
console.log(names);
In my HTML file, I have an input field with id=locationName and a button with onclick=saveData().
Can anyone point me towards what might be going wrong here?