Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Javascript - Property Map - Function reference

I have a JSON object in Javascript like given below:

var myjson={"name":"myname","address":"Myaddress"}
// if my json is not empty: 
         //do something

I want to find if the object is empty, that is if there are any keys present. How can I do that in Javascript?

Searched in vain through Google Search, but it is tough to say what to search for with so much junk (irrelevant) information coming up.

Is there any official reference for how this works (including delete a key) ? Or am I supposed to fight with Google Search (no bling for me) and hope for some relevant information?

like image 896
SijuMathew Avatar asked Dec 27 '22 21:12

SijuMathew


1 Answers

If you have ECMAScript 5:

Object.keys(myjson).length

will tell you how many (enumerable) properties exist in the object.

See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys, where there's also a version you can put in your own code if your browser doesn't support it natively.

like image 186
Alnitak Avatar answered Jan 06 '23 08:01

Alnitak