-1

Possible Duplicate:
How do I test for an empty Javascript object from JSON?

var test= {};

var incidentReport = {
      "place1": "n/a",
      "place2": "n/a",
      "place3": "n/a",
 }

Above are the two ways my varible is going to look. Ive tryed doing the following code to test if its empty/looks like {}

if(test == "")

and tried

if(test == null)

also tried

if(!test)

Does anyone know where I am going wrong? Just a beginner to JavaScript and JSON. Is what I am doing considered back practice are there better ways to declare this empty?

Thanks for the support

Community
  • 1
  • 1
Lemex
  • 3,772
  • 14
  • 53
  • 87
  • 1
    `Object.keys(obj).length == 0`. – Rob W Jun 22 '12 at 09:34
  • Your question is a dupplicate of http://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object-from-json, which has two excellent answers – Samuel Rossille Jun 22 '12 at 09:35
  • @RobW. Then why jQuery implemented it this way: `isEmptyObject: function( obj ) { for ( var name in obj ) { return false; } return true; }, ` – gdoron Jun 22 '12 at 09:36
  • @gdoron I would be happy to know why... Please tell if you find the answer to this question. – Samuel Rossille Jun 22 '12 at 09:37
  • @gdoron because jQuery explicitly refuses to work with code that modifies `Object.prototype` http://docs.jquery.com/Won't_Fix#Object.prototype_Issues – Esailija Jun 22 '12 at 09:39
  • @gdoron jQuery is designed to be cross-browser compatible. [`Object.keys`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys#Browser_support) is not available in FF 3.6, – Rob W Jun 22 '12 at 09:39
  • @gdoron "Introduced in JavaScript 1.8.5" is not relevant. JS 1.8.5 means nothing in a non-Firefox browser. – Rob W Jun 22 '12 at 09:41
  • This should not of been closed, got alot better answer here then i did in the link you provided – Lemex Jun 22 '12 at 09:41
  • @user983969 the answers here are copy pasted from the duplicate question... I don't know how to deal with that. I don't like it. – Esailija Jun 22 '12 at 09:42
  • @RobW. The link does. Anyway, it's a dup comment... :) – gdoron Jun 22 '12 at 09:42
  • Yes but ive also been explaiend to be where i was going wrong by @Eswar Rajesh Pinapala – Lemex Jun 22 '12 at 09:44
  • So ive learned, not just copied and pasted stacker overflow code :) – Lemex Jun 22 '12 at 09:45

3 Answers3

2

Use JSON.stringify

var test= {};
if(JSON.stringify(test).length==2)
alert('null')
1
if(test == "")

checks if it is an empty string, so this won't work

if(test == null)

checks if it is null which is "similar" to undefined - this isn't the case

if(!test)

checks if it is a falsy value, this in not the case either.

You have to check if there exist child-elements (properties):

function isEmpty(obj) {
    for(var prop in obj) {
        if(obj.hasOwnProperty(prop)) return false;
    }
    return true;
}

if ( isEmpty(test) ){...}

The very important point is the .hasOwnProperty() - this checks if it is a real property of the object and not only inherited through the prototype chain.

Christoph
  • 50,121
  • 21
  • 99
  • 128
  • @Somebodyisintrouble both? It explains why his attempts fail and gives a solution. Feel free to upvote me:-D – Christoph Jun 22 '12 at 09:39
0

test here is an object. so you have to check if there are any prioperties/elements int his object. You can try something like below

var test= {};

function isEmptyObject(obj) {
   // This works for arrays too.
   for(var name in obj) {
       return false
   }
   return true
}

alert("is this object empty?" + isEmptyObject(test));​
Eswar Rajesh Pinapala
  • 4,841
  • 4
  • 32
  • 40