Reputation: 231
I am trying to get the length or total objects inside array. I am creating a slickEventData object and pushing this object into eventsArray. So i end up with an array containing objects. I want to get the total amount of slickEventData objects inside this array. When i use length i get the wrong number, i get the total amount of keys? not objects.
My Code
var slickEventData = {}
console.log(event);
slickEventData.module = "slick_module"
slickEventData.eventType = event.type;
slickEventData.leadboxId = "a"//container.getAttribute("data-leadbox-id");
slickEventData.advertId = advertId;
slickEventData.length = "length of event"
slickEventData.time = Date.now();
slickEventData.posted = "postedStatus"
eventsArray.push(slickEventData)
console.log("events Array " + JSON.stringify(eventsArray))
console.log("events Array length " + eventsArray.length)
if (!sessionStorage.events) {
console.log("no old events found")
sessionStorage.events = eventsArray;
console.log(sessionStorage.events);
}
else if (sessionStorage.events) {
var oldEvents = sessionStorage.events;
console.log("old events length " + oldEvents.length);
console.log("updated events array met oldData " + eventsArray);
alert(JSON.stringify(oldEvents));
//loop thru old events to add them to current events and add these to the sessionStorage
//postTrackingData(sessionStorage.events);
}
//sessionStorage.events = eventsArray;// add events to storage
console.log("session = " + (JSON.stringify(sessionStorage)))
These are my attempts. I am guessing it is counting all the keys and values and the array as objects this returning 15 objects when i use oldEvents.length. eventsArray.length returns the correct value. Does adding this array into the sessionStorage.events mess this up?
How would i go about finding how many objects are inside oldEvents?
I might be doing this completely wrong
Edit:
oldEvents returns [object Object] so no multiple objects it seems eventsArray returns [object Object],[object Object] etc. so multiple objects.
arent they supposed to return the same? I am just adding eventsArray to the sessionStorage and then retrieving it
My goal is to keep pushing events into the sessionStorage as they happen and not loose them between refreshes.
Upvotes: 0
Views: 260
Reputation: 4766
[object object]
is what you get when you call .toString()
on an object.
You can't expand a string in devtools. I'd take the angle of your doing some unintentional type conversion.
This sessionStorage.events = eventsArray
and oldEvents = sessionStorage.events
looks suspicious to me.
LocalStorage and SessionStorage only accepts key value pairs of strings. It will not stringify your objects for you, but call .toString()
of them. I believe the issue is your not storing the stringified array into SessionStorage so when you get it out its not an array but a string, your calling .length
of a string.
May I suggest these changes:
//Setting storage items
sessionStorage.setItem('events', JSON.stringify(eventsArray));
//reading storage items
JSON.parse(sessionStorage.getItem('events'));
EDIT: Whilst this may not be directly answering the question, it is still (i believe) valuable information and too big to explain in a comment.
Upvotes: 1