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.
array.lengthis always going to return the number of items/objects inside that array. Are you initializing theeventsArraycorrectly?