I looked at some examples, but can't seem to figure this out. Basically I have a contact form in an ionic app that allows a user to contact a listing owner.
When they submit the form I want to store the ad id in local storage so they can't repeatability submit it over and over.
I need to be able to store json array and then check the results. If the ad id is in session storage don't show the form else show it.
I am currently doing this, which seems to store the ad ids in an array, but how do I loop through to check if an id exists? I tried angular forEach, but results e 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 ing as an object how can I get the key value?
if(value == adId){
//form has been submitted before
}else{
// showformVar = true
console.log(key + ': ' + value);
});
My storage looks like this
[{"id":"100033"},{"id":"100035"},{"id":"1000336"}]
How do I get id value? (e.g 1000033)
I looked at some examples, but can't seem to figure this out. Basically I have a contact form in an ionic app that allows a user to contact a listing owner.
When they submit the form I want to store the ad id in local storage so they can't repeatability submit it over and over.
I need to be able to store json array and then check the results. If the ad id is in session storage don't show the form else show it.
I am currently doing this, which seems to store the ad ids in an array, but how do I loop through to check if an id exists? I tried angular forEach, but results e 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 ing as an object how can I get the key value?
if(value == adId){
//form has been submitted before
}else{
// showformVar = true
console.log(key + ': ' + value);
});
My storage looks like this
[{"id":"100033"},{"id":"100035"},{"id":"1000336"}]
How do I get id value? (e.g 1000033)
Share Improve this question edited Jul 26, 2016 at 5:18 limit asked Jul 26, 2016 at 5:07 limitlimit 6472 gold badges8 silver badges27 bronze badges 5-
what are you getting when you do
var values = JSON.parse(localStorage.getItem("store_owner_ad_contacts"));
can you post it here? – Raman Sahasi Commented Jul 26, 2016 at 5:14 - Updated my question. I am getting - [{"id":"100033"},{"id":"100035"},{"id":"1000336"}] How do I get the id value? (e.g 1000033) – limit Commented Jul 26, 2016 at 5:20
-
if you don't like
angular.forEach
then you can use simple javascript's for loop as described in @Sasank's answer – Raman Sahasi Commented Jul 26, 2016 at 5:21 - If I console.log(values) I get an object returned. Looking in localStorage its storing as [{"id":"100033"},{"id":"100035"},{"id":"1000336"}]. I tried for loop same result. I must be missing something. – limit Commented Jul 26, 2016 at 5:24
- you can use forEach jsfiddle/nxfmxqge – singhakash Commented Jul 26, 2016 at 5:28
3 Answers
Reset to default 4In your loop you have an object, so just access id property like you do with an object: object.yourProperty or object[yourProperty]
var values = JSON.parse(localStorage.getItem("store_owner_ad_contacts"));
values.forEach(values, function(item, i) {
// you get object like this {id: "100033"}
// so to access id do like normal object
if (item.id === '100033') {
// do something
}
});
// inside forEach loop
var k = 0;
for(k in value) {
// u can get the key in this way (k)
}
As I see this is not about not about angular itself (using this in an angular doesn't mean relation). If you have an array of values and want to search for something then you can use Array.prototype.find
.
if your data is stringified array then you should parse it before with JSON.parse
.
If you're about to use localStorage in your angular app then that would be better to use ngStorage which handles stringify and parse and some more options.
var lsItems = [{"id":"100033"},{"id":"100035"},{"id":"1000336"}];
var item = lsItems.find(i => i.id === "100035");
console.log(item)