0

I am testing for a condition such that if there are no items in HTML 5 local storage.

if (localStorage.getItem("Cart_items")) {
 //do something....
}

If there are no items, it is still displaying

1 item in Storage Cart_items="{}"

So that my condition is always not NULL..The empty object is treated as an item hence not NULL

How can i write code to know whether Cart_items contains values or not ???

Update:

Resolved the issue as follows:

function isObjectEmpty(object)
{
var isEmpty = false;
for(keys in object)
{
 isEmpty = true;
 break; // exiting since we found that the object is not empty
}
return isEmpty;
}


var myObject = JSON.parse( localStorage.getItem("Cart_items") );//object
var isEmpty  = isObjectEmpty(myObject); 
// check if the object is not empty
isEmpty  = isObjectEmpty(myObject); 
if (isEmpty){
//do something
}
user3790186
  • 239
  • 6
  • 21

0 Answers0