0

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.

5
  • It doesn't matter what's inside an array; the array.length is always going to return the number of items/objects inside that array. Are you initializing the eventsArray correctly? Commented May 4, 2016 at 11:50
  • Try to add a console.log(oldEvents) statement (no JSON.stringify). In Chrome or Firefox you will be able to browse through you array and check it's contents conveniently. Make sure the array contains what you expect. Commented May 4, 2016 at 11:52
  • Hmm oldEvents returns [object Object]. But eventsArray returns [object Object],[object Object] etc. Commented May 4, 2016 at 11:57
  • console.log(<yourObjectOrWhatever>) or .dir() should show you all the objects in a tree-like, exapandable hierarchy. This way you can exactly see what your variable contains. Use Chrome for this. Eventually you have an extra layer e.g. Array[Array[Obj,Obj]] instead of the expected Array[Obj,Obj]. Check stackoverflow.com/questions/4482950/… Commented May 4, 2016 at 12:31
  • Yeah i think this is the problem aswell but for some reason chrome doesnt always let me expand the object like it should. i think im going to download firebug see if that will work better. Like i said it returns [object Object] but it wont let me expand it Commented May 4, 2016 at 12:46

1 Answer 1

1

[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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes i just figured out what i was doing wrong. Now im explicitly going to stringify it before and parse it after. Since you kind of solved the problem i thought id mark this as the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.