Forum Moderators: open

Message Too Old, No Replies

Emptying an object, set to {} or false?

         

csdude55

9:36 pm on Aug 26, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have an object that's set like this:

var getObj;

getObj = JSON.parse(
typeof(Storage) !== 'undefined' ?
sge.getItem(saveName) : // sge and saveName are set earlier
getCookie(saveName) || false // getCookie is a separate function to read a cookie; if no cookie is found it returns ''
);

if (
Object.values(getObj).length > 0 &&
(getObj.timestamp + 60000) > Date.now() // expired
) {
sge.removeItem(saveName);
setCookie(saveName, '');

// does this empty the object?
getObj = {};
}


in the JSON.parse() section, if the type is not undefined then I set getObj to the results of .getitem(); if it's undefined then I try to set it to the results of getCookie(). But if there's no cookie... there's where I have the question.

I would THINK that I should set it to {}; eg, declare it as an empty object. Which is what I do in the next-to-last line, which I think empties the object.

But getCookie(saveName) || {} throws an error that object Object is not valid JSON.

If I should be setting it to false (like in the code above), then does that next-to-last line also need to be getObj = false instead of getObj = {}?

csdude55

9:51 pm on Aug 26, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sigh. After thinking about this one all day, it clicked after I posted. That's twice now! I'm gonna have to stop posting before I make a bigger fool of myself.

Duh. JSON.parse converts the string to the object, so of COURSE it needs to be "false" as a final default.

I'm taking the rest of the day off and eating cake, I need a mental break :-/