I have been trying to solve this issue by looking at various examples, but haven't been able to figure it out. In my Ionic app, I have a contact form that allows users to contact a listing owner.
After the form submission, I want to store the ad id in local storage to prevent repetitive submissions. I need to store the ad id in a JSON array and then check if it already exists in the session storage before displaying the form.
Currently, I am storing the ad ids in an array, but I am struggling to loop through and check if an id already exists. I attempted to use angular forEach, but the results are coming in as an object.
// Parse any JSON previously stored in allEntries
var existingEntries = JSON.parse(localStorage.getItem("store_owner_ad_contacts"));
if(existingEntries == null) existingEntries = [];
var adId = {
"id":$scope.adId
};
// Save allEntries back to local storage
existingEntries.push(adId);
localStorage.setItem("store_owner_ad_contacts", JSON.stringify(existingEntries));
var values = JSON.parse(localStorage.getItem("store_owner_ad_contacts"));
angular.forEach(values, function(value, key) {
// ^ This is coming as an object how can I get the key value?
if(value == adId){
//form has been submitted before
}else{
// show formVar = true
console.log(key + ': ' + value);
});
This is how my storage currently looks:
[{"id":"100033"},{"id":"100035"},{"id":"1000336"}]
I am struggling to retrieve the id value from the JSON array (e.g. 1000033). How can I do this?